Yii2 Override AssetConverter Class - yii2

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.

Related

Yii 2 Module wise configuration setting

I have setup Yii2 nested module and I want to set different configuration and each modules has own component and other settings with there own models
Like in School Management System, I have created a nested module like V1 is my API (main module) and under these, I have created a Student module, Teacher module, Parent module, driver module, admin module each has a different table and different model. I want to login differently with each user like..
API calls for each
https://example.com/v1/admin/login
https://example.com/v1/student/login
https://example.com/v1/parent/login
https://example.com/v1/user/login
https://example.com/v1/driver/login
How can I manage these login and their own configuration?
Thanks
Jitendra
In the module Class you can set components, aliases and other settings and using something like this:
Here is an example for the admin module:
class Admin extends Module
{
// ...
/**
* {#inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
$this->setComponents([
// array of components
]);
// ...
}
}
or if you prefer you can set the components like this
$this->components = [
// array of components
]);
if you are using nested modules, you are already specifying "settings" for the module - i.e all sub modules
To use different login for all modules when specifying components set different name for the identityCookie of the user component for each module.
Example for admin module:
$this->components => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-admin', 'httpOnly' => true],
],
// ... other components

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 How to enable default assets only in certain modules

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