Yii2 How to enable default assets only in certain modules - yii2

I am using basic yii2 app, I have disabled the boostrap css and js in web.php config but i want to enable it inside the module called admin.
I tried adding like this, for component section in config.php under admin module folder, but no luck!
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapPluginAsset' => [
'js'=> ['js/boostrap.js']
],
'yii\bootstrap\BootstrapAsset' => [
'css' => ['css/boostrap.min.css']
]
],
],
How can i achieve this?

In your module's init() method, you can override an exist asset bundle like below:
public function init() {
\Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset'] = [
'css' => ['css/bootstrap_NEW.css']
];
parent::init();
}
In above example, we override yii\bootstrap\BootstrapAsset CSS file in our module. So, in this module, it uses bootstrap_NEW.css instead of its default.
Please note that, you can do this in application globally, not specially in module's init() method. It was just a suggestion to solve this specific case.

You should use asset bundles to handle this, no need to modify assetManager.
To disable Bootstrap, simply remove it from $depends in AppAsset :
public $depends = [
'yii\web\YiiAsset',
];
Create a ModuleAsset class in your module :
class ModuleAsset extends \yii\web\AssetBundle
{
public $depends = [
'yii\bootstrap\BootstrapAsset',
];
}
Make sure to register it in your module's views (you could use a custom layout) :
ModuleAsset::register($this);
Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

Related

appendTimestamp and registerAssetBundle

I am using the following configurations to add the timestamp in the assets path via asset manager.
'assetManager' => [
'appendTimestamp' => true,
appAsset.php
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'/css/style.css'
];
}
In view
AppAsset::register($this);
But in html I still see '/css/style.css' without timestamp.
I saw solution
https://github.com/yiisoft/yii2/issues/9101#issuecomment-267274327
$file = '/css/style.css';
$asset = new \yii\web\AssetBundle(
[
'js' => [ltrim($file, '/')],
'basePath' => '#webroot',
'baseUrl' => '/'
]
);
$this->getAssetManager()->bundles[$file] = $asset;
$this->registerAssetBundle($file);
This example is work. '/css/style.css?v=12557565'
Is there any method write registerAssetBundle() not in a view files?
First of all, what is public $sourcePath = '#frontend/assets';? it is not the correct path this path actually has your Asset.php files, not the source files. According to the documentation
sourcePath: specifies the root directory that contains the asset
files in this bundle. This property should be set if the root
directory is not Web accessible. Otherwise, you should set the
basePath property and baseUrl, instead. Path aliases can be used here.
Just place your .css files under frontend/web/css and .js under frontend/web/js and remove the sourcePath you do not need it as looking at your code, only in case if you had your source files under frontend/themes/basic then you would use sourcePath like public $sourcePath = '#frontend/themes/basic/'; because this path isnt under the web accessable directory
And for adding the timestamp You do not have to do anything other than adding the appendTimestamp options under the components section in the assetManager, make sure you are adding assetManager under components. and it will automatically start adding ?v=125453612 with all CSS and js files
your AppAsset.php
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/style.css'
];
}
frontend/config/main.php
'components' => [
..........
..........
'assetManager' => [
'appendTimestamp' => true
]
..........
..........
],
and your file will start looking like this
I can show you a live example of the same site if you want to have a look into the view-source
EDIT
As you provided the source code screen grab and looking at your code I can tell that you are using trailing slash / with your files, remove the trailing slashes from your source files path, I also overlooked it the first glance but then looking the code i couldn't find any other thing than this one that could be wrong. they should be like this
'css/new.css',
'css/new2.css',
and same for the js files too.
Try write public $css and public $js without forst slash ("js/.." instead "/js/..").

Yii2 custom gii generator and template?

I just implementing a new gii generator for my requirement on yii2.
i want to know best place to keep those codes?
Create app\modules\gii directory with own Generator class, views, templates.
namespace app\modules\gii;
class MyCustomGenerator extends \yii\gii\generators\crud\Generator
{
// ...
public function generate()
{
// ...
}
}
Then enable it in gii configuration.
[
// ...
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
'generators' => [
'class' => '\app\modules\gii\MyCustomGenerator',
'model' => ['class' => '\app\modules\gii\model\MyCustomGenerator'],
],
],
],
]
Which module of gii that you need to custom?
If it is CRUD module, I think you should put it into app/backend/ because it just use for this app.
Here is my customization: custom crud gii templates
My folder structure: yii2 - custom gii crud templates - folder structure
No good idea own gii template put in app/modules/gii. Better is create separate module. Benefits:
can reuse in other projects
if put it in composer under require_dev, do not install on production

Yii2 AssetBundle publishOptions pattern syntax?

Might be stupid, but just wanted to ask if the syntax I'm using is correct when publishing asset's files. The Guide instructs to use folder names without trailing asterisk, however for me it does not work that way.
The weird thing is even if I specify to only publish css and images folder, the assetManager also publishes other folders in $sourcePath (luckily without files inside). What's more, when new files are added to images folder, they remain unpublished until I delete #web/assets folder. Is this to be expected?
<?php
namespace app\views\layouts\main\assets;
use yii\web\AssetBundle;
class ThemeAsset extends AssetBundle
{
public $sourcePath = '#theme';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $publishOptions = [
"only" => [
"css/*",
"images/*",
],
"forceCopy" => false,
];
}
The first note that you should set $sourcePath or $baseUrl not both of them (If you set the both, the former overwrites the last when assetManager calls publish method).
The weird thing is even if I specify to only publish css and images
folder, the assetManager also publishes other folders in $sourcePath
(luckily without files inside).
If you wanna avoid asset manager to publish this folders, you must mention them on except parameter of publisOptions:
'except' => [
"doc/",
"img/",
],
What's more, when new files are added to images folder, they remain
unpublished until I delete #web/assets folder. Is this to be expected?
Yes! It's a natural behavior. You may set forceCopy property of publishOptions to true in development mode. This causes to update asset contents in each refresh. Easily you can use Yii production constant: YII_DEBUG.
public $publishOptions = [
'only' => [
'js/',
'css/',
],
'forceCopy' => YII_DEBUG,
];
So totally you have an asset manager like this:
<?php
namespace app\views\layouts\main\assets;
use yii\web\AssetBundle;
class ThemeAsset extends AssetBundle
{
public $sourcePath = '#theme';
public $css = [
'css/site.css',
];
public $publishOptions = [
"only" => [
"css/*",
"images/*",
],
'except' => [
"doc/",
"img/",
],
"forceCopy" => YII_DEBUG,
];
}

Theme based module's views in yii2

I am using Yii2 Advanced Template. I have created user module inside /frontend/modules/ directory. without theme integration, views are called from /modules/user/views/ directory.
Currently, I have created three different themes inside /frontend directory. So I would like to access views from theme directory for User Module. How it would be possible?
It is possible to set layout for module by
$this->layoutPath = \Yii::getAlias('/themes/classic/views/layouts/');
$this->layout = 'userLayout';
But How views can be accessed from theme directory for module?
Please suggest possible solutions..
I found solution to my question. I have implemented following way.
Added Layout path in Module.php after init() method.
public function init()
{
parent::init();
$this->layoutPath = \Yii::getAlias('#app/themes/classic/views/layouts/');
$this->layout = 'userLayout';
}
and
added theme configuration with module views in main.php config file
'view' => [
'theme' => [
'pathMap' => [
'#app/views' =>'#app/themes/classic/views/',
'#app/modules'=>'#app/themes/classic/modules',
'#app/widgets'=>'#app/themes/classic/widgets'
],
'baseUrl'=>"/themes/classic",
],
],

Yii2 Override AssetConverter Class

In my main-local.php i have the following code:
'assetManager' => [
'converter' => [
'class' => 'yii\web\AssetConverter',
]
]
Now I want to override the default AssetConverter with my own Converter. When I change the class to custom\web\AssetConverter it does not find the class.
I created a directory under vendor\custom\web with AssetConverter.php file and a namespace custom\web.
Where do I have to put my own AssetConverter file to be founded? And do I have to register the vendor somewhere, so that the namespace can be found?
Your class namespace is incorrect and also the vendor directory for composer packages.
For example create components or folder into project root folder.
In this case your class namespace should like this (related to basic application template):
namespace app\components;
Then include it to config:
'assetManager' => [
'converter' => [
'class' => 'app\components\AssetConverter',
]
]
Also, your AssetConverter should extends \yii\web\AssetConverter in over case you will get error, what class not found.