You know, yii2 with adminlte is exist in this git .
But if I use this using composer, it not gives me an educate.
You know, I want to learn step by step.
So I tried like this:
I download adminlte in his official.
In this zip file, I have one folder with name : "AdminLTE: 2.*"
Then I Extract all the item in folder AdminLTE:2.* into vendor/bower/adminLTE/"bunch of file"
Now, I edit assets/AppAsset like this:
class AppAsset extends AssetBundle {
public $basePath = '#webroot';
public $baseUrl = '#web';
public $sourcePath = '#bower/'adminLTE';
public $css = [
'adminLTE/dist/css/AdminLTE.min.css',
'css/site.css',
];
public $js = [
'adminLTE/dist/js/app.js'
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
];
}
But, both css and js below is 404 ?
Any help it so appreciated
If you want to manually add AdminLTE there is no need to put it in the bower folder.
Place it in the /web folder (in case of Advanced Template it's /frontend/web or /backend/web).
You have got AppAsset already set for this folder but you have to remove $sourcePath because when this is set $basePath and $baseUrl are overriden.
Do not add theme inside the vendor for manual installation of theme. Follow below mentioned steps.
Step 1 : Download your adminlte and create a directory named theme inside a root
ex. your_project/basic/themes ( i.e outside the web directory)
Step 2 : paste your adminlte theme folder inside newly created theme directory
Step 3 : Now you need to create asset to register all the csss and js required for adminlte theme
go to your_project/basic/assets directory and create a new file lets say, AdminLTEAsset.php
namespace app\assets;
use yii\web\AssetBundle;
/**
* #author Qiang Xue <qiang.xue#gmail.com>
* #since 2.0
*/
class AdminLTEAsset extends AssetBundle {
public $sourcePath = '#app/themes/adminlte/';
public $css = [
'dist/css/AdminLTE.css',
'font-awesome-4.5.0/css/font-awesome.min.css',
'ionicons/2.0.1/css/ionicons.min.css',
'dist/css/skins/_all-skins.min.css',
];
public $js = [
'dist/js/app.js',
'plugins/slimScroll/jquery.slimscroll.min.js',
'plugins/fastclick/fastclick.min.js',
'dist/js/demo.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
];
}
Step 4: Now go to the themes/adminlte/layouts/main.php and register your newly created AdminLTEAsset. as follows.
$assets = app\assets\AdminLTEAsset::register($this);
$basUrl = $assets->baseUrl;
Step 5 : in your /config/web.php file add the following pathmap
'components'=>[
'view' => [
'theme' => [
'pathMap' => ['#app/views' => '#app/themes/adminlte'],
'baseUrl' => '#web/../themes/adminlte',
],
],
]
Embeding admin lte theme i yii2 basic
1) create yii project using composer
sudo du
cd /var/www/html
composer create-project yiisoft/yii2-app-basic basic 2.0.4
2)now create it accessible
chmod 777 -R project name
3) download admin lte theme by using
git clone https://github.com/bmsrox/baseapp-yii2basic-adminlte.git
4) now update composer
composer update
if token error then create account in git and create token in setting tab by clicking genarate new token
copy and provide it to composer
5) update your web.php file in config
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'layout'=>'column2',
'layoutPath'=>'#app/themes/adminLTE/layouts',
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'site/index',
'<action:(index|login|logout)>'=>'site/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
],
],
'view' => [
'theme' => [
'pathMap' => ['#app/views' => '#app/themes/adminLTE'],
'baseUrl' => '#web/../themes/adminLTE',
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'n0VkMX1RmIa_ovJmwR3Gn_hdZyQ7SyKe',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
//$config['modules']['gii'] = 'yii\gii\Module';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'generators' => [ //here
'crud' => [ // generator name
'class' => 'yii\gii\generators\crud\Generator', // generator class
'templates' => [ //setting for out templates
'custom' => '#vendor/bmsrox/yii-adminlte-crud-template', // template name => path to template
]
]
],
];
}
return $config;
6) update SiteController. Php in controller folder
replace actionLogout with following code
public function actionLogout()
{
Yii::$app->user->logout();
return $this->redirect(Yii::$app->user->loginUrl);
}
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
// change layout for error action
if ($action->id=='login')
$this->layout = 'login';
return true;
} else {
return false;
}
}
7) if misconfiguration error then
update apche2
by using command
a2enmod rewrite
and restrart apache using
service apache2 restart
finished...........
best of luck
Related
Trying to run function _googleanalytics in controller ProcessingController, but getting an error:
unknown command
command:
./yii processing/_googleanalytics '2017-02-27' '2017-02-27'
controller path:
/console/controllers/
action
public function _googleanalytics($start, $finish) {...
controller
namespace console\controllers;
class ProcessingController extends Controller
{...
/console/config/main.php
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures',
],
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning','info'],
'exportInterval' => 1,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'exportInterval' => 1,
'logVars' => [],
'categories' => ['calls'],
'logFile' => '#app/runtime/logs/calls.log',
'maxFileSize' => 1024 * 2,
'maxLogFiles' => 20,
],
],
],
],
'modules'=>[
'user-management' => [
'class' => 'webvimark\modules\UserManagement\UserManagementModule',
'controllerNamespace'=>'vendor\webvimark\modules\UserManagement\controllers', // To prevent yii help from crashing
],
'googleanalytics' => [
'class' => 'console\modules\googleanalytics\Module',
]
],
'params' => $params,
];
what I am doing wrong?
You need to make an action to access it via console/terminal the same way as we access the actions via our browser.
For example if i create a Test Controller like below inside console/controllers directory
<?php
namespace console\controllers;
class TestController extends \yii\console\Controller{
public function actionIndex($param1,$param2){
echo "\nIndex";
echo "\n$param1 $param2\n";
}
public function actionMango(){
echo "\nMango";
}
}
and then type ./yii and hit Enter it will show all default commands available along with the following at the end.
This is Yii version 2.0.14.1.
The following commands are available:
....
...
- test
test/index (default)
test/mango
which means it registers all the actions inside the controller as commands and if you write in the terminal the following command,
./yii test/index omer aslam
it will show you the output
Index
omer aslam
where omer and aslam are the 2 params passed to the function.
So you just need to prepend keyword action to your function name i would suggest using action names according to the convention, change the function from
public function _googleanalytics($start, $finish) {
to
public function actionGoogleanalytics($start, $finish) {
and then access it via
./yii process/googleanalytics 2017-02-27 2017-02-27
you can wrap with quotes but it isnt necessary to add one a space identitifies between separate params.
Hope it helps
i was trying to make
this url
http://localhost/advanced/frontend/web/index.php?r=test
into
htttp://localhost/advanced/frontend/web/tests in yii2
also to receive post request
this in my codding in common/config/main.php
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
],
'urlManagerFrontEnd' => [
'enablePrettyUrl' => true,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['test'],
'only' => ['index', 'create'],
],
],
'showScriptName' => false,
],
],
];
this in my frontend testController
namespace frontend\controllers;
use Yii;
use yii\rest\ActiveController;
use common\models\Test;
use yii\data\ActiveDataProvider;
class TestController extends ActiveController {
public $modelClass = 'common\models\Test';
public function actions(){
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function actionCreate(){
$model = new Test();
$model->load(Yii::$app->request->post(),'');
$model->save();
return $model;
}
}
i have a problem in the post request when i tried
Postman result jpg
so i have 2 problem the first is the prettyUrl and the post request
if u have a solution please make it easy because i just learn php yii2 html css etc for 1 month
I'm foregoing Yii2's bundled jQuery and Bootstrap assets in exchange for ones I've bundled myself using npm/browserify So step one was to remove jQuery and Bootstrap from yii\web\YiiAsset via the config:
'components' => [
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapAsset' => [
'css' => [],
],
],
],
...
]
I want to put this in the footer, obviously, but it needs to load before any other assets so that jQuery is available to them.
Here's my AssetBundle:
class AppAssets extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $depends = [
'app\assets\CustomAssets',
'yii\web\YiiAsset',
];
}
With my CustomAssets bundle (that contains my own JS and CSS) declared first in depends, I think this would work, but I also have an ActiveForm on the page, and this registers its own asset bundle, which is dependent on the various Yii assets. Here's where I embed that form:
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
...
<?php ActiveForm::end(); ?>
Is there a way I can override the depends on ActiveFormAsset so that it also depends on my scripts?
Since asset bundle dependencies are transitive, it occurred to me that in the same way I'm overriding the config for the Jquery and Bootstrap bundles, I can override the config for yii\web\YiiAsset like so:
'components' => [
'assetManager' => [
'bundles' => [
...
'yii\web\YiiAsset' => [
'depends' => [
'app\assets\CustomAssets',
],
],
],
],
...
]
So when ActiveFormAsset tries to load yii\web\YiiAsset, it processes the dependency on my own scripts/styles, and also covers any other assets that get loaded and might depend on yii\web\YiiAsset.
what i did was replacing shipped assets, instead of killing them:
'bundles' => [
'yii\bootstrap\BootstrapAsset' => ['class' => 'common\assets\BootstrapAsset',],
'yii\bootstrap\BootstrapPluginAsset' => ['class' => 'common\assets\BootstrapPluginAsset',],
],
Yii2 Theme Integration ?
'view' => [
'theme' => [
'pathMap' => ['#app/views' => '#app/admin/views'],
'baseUrl' => '#web/admin',
],
],
Hope you are using the advanced template
add a folder themes in the backend folder
make a subfolder with the theme name and make sure you have the layouts folder in that folder
ie
your new layout folder path will be
backend/themes/themefoldername/layouts
in the folder backend/config/main.php
'components' => [
'view' => [
'theme' => [
'basePath' => '#backend/themes/themefoldername',
'baseUrl' => '#backend/themes/themefoldername',
'pathMap' => [
'#backend/views' => '#backend/themes/themefoldername',
],
],
],...
if you want to keep it in the web folder also you can do that,but make sure you change the path accordingly
In advance template there is separate configuration for frontend and backend theme integration.
Frontend theme integration => "frontend/config/main.php" file :
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#frontend/views' => '#themes/frontend/views', // need to // set alias first in your bootstrap.php file
],
],
],
],
Backend theme integration => "backend/config/main.php" file :
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#backend/views' => '#themes/backend/views', // need to set // alias first in your "common/config/bootstrap.php" file
],
],
],
],
While coding take care of comments and directory paths and no need to write baseUrl or basePath.
create "themes" directory in web directory and create theme there.
then include this code in your main config file.
'view' => [
'theme' => [
'baseUrl' => '#web/themes/yourthemename',
'pathMap' => [
'#app/views' => [
'#webroot/themes/yourthemename/views',
]
],
],
]
use this code in your web.php file.
'view' => [
'theme' => [
'class' => yii\base\Theme::className(),
'basePath' => '#app/themes/themename',
'baseUrl' =>'#web/themes/themename',
],
],
Here is my code which i normally use for themeing. You can set param in params file and add theme name there or directly in the below code.
'view' => [
'theme' => [
'pathMap' => ['#app/views' => '#webroot/themes/themename/views'],
'baseUrl' => '#web/themes/themename',
],
],
if you are using yii2 basic then in config/web.php write this
return [
'components' => [
'view' => [
'theme' => [
'basePath' => '#app/themes/basic',
'baseUrl' => '#web/themes/basic',
'pathMap' => [
'#app/views' => '#app/themes/basic',
],
],
],
],
];
I have the adminlte theme this be find the vendor folder,
then in the config/main.php added this:
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#app/views' => '#vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app'
],
],
],
In basic installation under config/web.php add the code under component block.
'components' => [
................
....................
'view' => [
'theme' => [
'pathMap' => [
'#app/views' => '#app/themes/mytheme',
'#app/modules' => '#app/themes/mytheme/modules',
],
'baseUrl' => '#web/themes/mytheme',
],
],
...........
]
Refer below link for install theme and setup.
http://banoprogrammer.blogspot.in/2017/07/backend-theme-installation.html
I set up a theme for my frontend using the advanced template. My themes are located in the themes folder which I created for storing the themes. eg. web/themes/cerulean. There are NO physical view folders under any of the individual theme folders as might be suggested by some of the key/value pairs that I have seen eg. ['#app/views' => '#webroot/themes/themename/views]. In fact, my code runs with and without value's views subfolder. This is my working code => #webroot/themes/cerulean as opposed to #webroot/themes/cerulean/views but it does need key's views subfolder. ie. #app/views. I have tested both of these variations and they both work so do not be concerned whether you have a view on the end of the value or not.
Because I am using a theme for the frontend I have replaced #app/views as above with #frontend/views. This is my code in my frontend/config/main.php file.
'view' => [
'theme' => [
'pathMap' => ['#frontend/views' => '#webroot/themes/cerulean',],
'baseUrl' => '#web/themes/cerulean',
],
],
This is the code in my frontend\assets\appasset.php file:
namespace frontend\assets;
use yii\web\AssetBundle;
use Yii;
Yii::setAlias('#themes', Yii::$app->view->theme->baseUrl);
/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
//public $baseUrl = '#web';
public $baseUrl = '#themes';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
You will notice above that I have substituted the
public $baseUrl = '#web';
with an alias #themes which I have set at the top namely...
Yii::setAlias('#themes', Yii::$app->view->theme->baseUrl);
The baseurl in the code above is now set to #themes which actually represents #web/themes/cerulean' taken from the 'view' => 'theme' setting located in the main.php file under frontend/config.
We've been working on a project with Yii2 Advanced App, with a custom bootstrap template. I've generated the crud using gii. All other CRUDs work fine. But the User crud displays Yii2 User Module not the CRUD.
I've gone through amnah's complete documentations and couldn't find any solutions in any other places either. I even tried Yii2 documentations and it wasn't any help either.
This is my backend config
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => 'sahasa/index',
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'request' => [
'baseUrl' => $baseUrl,
],
'user' => [
'class' => 'amnah\yii2\user\components\User',
],
// 'user' => [
// 'identityClass' => 'common\models\User',
// 'enableAutoLogin' => true,
// ],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
'modules' => [
'user' => [
'class' => 'amnah\yii2\user\Module',
// set custom module properties here ...
],
'debug' => [
'class' => 'yii\debug\Module',
],
],
];
This is what I get when I goto localhost/app/backend/web/index.php?r=user
I want it to display a CRUD similar to this
I'm stuck there. Without the CRUD it'll be difficult to manage the USERS.
Any help is appreciated.
Thanks in advance.
I think you need to redefine the controller too. You need a new controller derived form the original controller for correctly addressing the new CRUD element.
I suppose your controller is in backend and is named user otherwise change properly the following example (part of config/main.php):
'user' => [
...
'controllerMap' => [
'user' => 'backend\controllers\user',
],
],
You can add this code to Controller in backend
public function init()
{
$user_id = Yii::$app->getUser()->id;
if($user_id){
$user = \amnah\yii2\user\models\User::findOne($user_id);
if ($user->can("admin")) {
// do something
}else{
throw new HttpException(403, 'You are not allowed to perform this action.');
}
}else{
throw new HttpException(403, 'You are not allowed to perform this action.');
}
parent::init();
}
Just click the link /user/admin
You'll get CRUD for users.