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',],
],
Related
I want to overwrite a css code of an existing asset, for example “kartik\form\ActiveFormAsset”. If i follow the official guide, i will do this:
'components' => [
'assetManager' => [
'bundles' => [
'kartik\form\ActiveFormAsset' => [
'sourcePath' => null, // do not publish the bundle
'basePath' => '#webroot',
'baseUrl' => '#web',
'css' => [
'/custom.css',
]
],
],
],
],
But it automatically changes a path to a js file which i want to keep up-to-date and don’t do any changes.
So, how to overwrite asset only for css, not for js?
If you don't need an asset assignment or you want overwrite the assignmente you could try using registering scripts in your view .
$this->registerCssFile("#web/css/themes/black-and-white.css", [
'depends' => [\yii\bootstrap\BootstrapAsset::class],
'media' => 'print',
], 'css-print-theme');
see yii2 guide and reference for this
https://www.yiiframework.com/doc/guide/2.0/en/output-client-scripts
https://www.yiiframework.com/doc/api/2.0/yii-web-view
I removed yiisoft/yii2-bootstrap from composer.json and update composer, removed bootstrap from assets/AppAsset.php, but still loads bootstrap CSS and JS!
When I remove <?php $this->head() ?> from layout, CSS doesn't load, but bootstrap.js is loaded!
How to remove all bootstrap assets from Yii2 basic app?
In web.php add this code
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js'=>[]
],
'yii\bootstrap\BootstrapAsset' => [
'css' => [],
],
],
],
and remove this line in AppAsset.php
public $depends = [
'yii\web\YiiAsset', #REMOVE
'yii\bootstrap\BootstrapAsset', #REMOVE
];
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
How to override the class module in the config of my theme? I tried so impossible.
return [
...
'modules' => [
'shop' => [
'class' => 'app\modules\shop\ShopModule',
'components' => [
'manager' => [
'class' => 'app\web\theme\modules\shop\Customer',
],
],
],
...
],
];
Overriding controllers
Sometimes you may need to override default Yii2-user controllers. It is pretty easy and takes two steps.
Step 1: Create new controller
First of all you need to create new controller under your own namespace (we’d recommend app\controllers\user) and extend it from needed Yii2-user controller.
For example, if you want to override AdminController you should create app\controllers\user\AdminController and extend it from dektrium\user\controllers\AdminController:
<?php
namespace app\controllers\user;
use dektrium\user\controllers\AdminController as BaseAdminController;
class AdminController extends BaseAdminController
{
public function actionCreate()
{
// do your magic
}
}
Step 2: Add your controller to controller map
To let Yii2-user know about your controller you should add it to controller map as follows:
<?php return [
...
'modules' => [
...
'user' => [
'class' => 'dektrium\user\Module',
'controllerMap' => [
'admin' => 'app\controllers\user\AdminController'
],
...
],
...
],
For overriding view click here
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.