How to customize vendor view files? - yii2

In yii2 how do I customize vendor view files without modifying the original view files?
I'm using dektrium yii2-user and would like to make a few changes to the login page.

You can assign your view path for dektrium yii2-user in this way (assume #app your app alias) :
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#app/views/your_dir_views' // mapping for override the views dektrium with your views
],
],
.....
],

Related

Yii2 Combining and Compressing Assets

I’m having trouble to combine and compress Yii2 assets.I am not able to solve the following scenario.
AppHeadAsset contains the main css for the application in the head position
AppEndJSAsset contains the JS for the application in the end position. Shared and essential code is supposed to be included here. This one depends on YiiAsset, BootstrapAsset
CommentAsset contains code only relevant to the comment process. Should be included as well with the above because it add custom functionality but only relevant to this section ,depends on AppEndJSAsset.
The asset configuration is given
'bundles' => [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
'frontend\assets\AppAsset',
'frontend\assets\AppHeadAsset',
'frontend\assets\AppJsEndAsset',
'frontend\assets\AppJsCommentAsset'
],
'appJsEndAsset' => [
'class' => 'frontend\assets\AppJsEndAsset',
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
'js' => 'js-end-result.js',
'css' => 'css-end-result.css',
'depends' => [
'yii\web\JqueryAsset',
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
],
],
'appJsCommentAsset' => [
'class' => 'frontend\assets\AppJsCommentAsset',
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
'js' => 'js-comment-result.js',
'css' => 'css-comment-result.css',
'depends' => [
'frontend\assets\AppJsEndAsset',
'frontend\assets\AppJsCommentAsset'
],
],
Even with the depends section it include commentAsset before the jquery and the AppEndJsAsset.
I’m not able to quite figure out what is the problem and the best way of doing this
Thanks!

How to correctly specify migration namespace classes in yii2?

could someone explain me how can I correctly specify my modules migration namespaces? As I see in the documentation, it's:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
],
];
But there are no explanation in which file should I write the commands. I've tried it in config.php, as:
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\modules\adBoard\migrations',
],
But I don't know which controller class should I write. Could someone tell me in which file I have to specify it and how to specify it correctly?
If you refer to this documentation
Configuring Command Globally
Instead of entering the same option
values every time you run the migration command, you may configure it
once for all in the application configuration like shown below:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationTable' => 'backend_migration',
],
], ];
With the above configuration, each time you run the migration command, the backend_migration table will be used to record
the migration history. You no longer need to specify it via the
migrationTable command-line option.
Namespaced Migrations
Since 2.0.10 you can use namespaces for the
migration classes. You can specify the list of the migration
namespaces via migrationNamespaces. Using of the namespaces for
migration classes allows you usage of the several source locations for
the migrations. For example:
return [
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'app\migrations', // Common migrations for the whole application
'module\migrations', // Migrations for the specific project's module
'some\extension\migrations', // Migrations for the specific extension
],
],
], ];
this config should be placed in you console/config/main.php
but for namespaced migration remeber that starting for 2.0.10
depending on your yii2-template application (basic or advanced) the location of "console" specific settings are located in different directories.
For basic template, console fetch settings from <app>/config/console.php file.
And for advanced template, you should edit <app>/console/config/main.php file.
Remember that your settings for console will not affect web-settings, so if you want to register some component in the whole project, you have to duplicate it in both files.
P.S. I would like to add one more detail about advanced template, is that it has common setting for frontend and backend sub-apps, which is located in <app>/common/config/main.php, but those settings are not common with console commands.
I would like to share my experience with Yii2 namespaced migrations.
Scenario
I am using advanced template.
I have 100s of old migrations in console/migrations folder.
I have new extension which have namespaced migrations.
I have new migrations in old folder console/migrations.
I want to create future migrations with namespaces in new folder console/migrations/namespaced. I want to keep all old migrations intact which are located at console/migrations.
console/config/main.php configurations worked for me.
return [
'controllerMap' => [
'migrate' => [
'class' => \yii\console\controllers\MigrateController::class,
'migrationNamespaces' => [
'console\migrations\namespaced',
'yii\swiftsmser\migrations'
]
],
],
//.... more configurations
];
With above configurations, when I executed yii migrate, It includes all above mentioned folders.
Note: To create new migration. Just make sure to use command like below.
yii migrate/create console\\migrations\\namespaced\\DLTTemplatesForSMS

Yii2 render database content

There is a textarea where user can edit some templates and can use variables like:
{{model.user.name}}
Application need to replace this variables with data and display HTML output.
We can write a small function that will replace variables from template with data but we need to use a template engine like Twig or Smarty.
https://github.com/yiisoft/yii2-twig
https://github.com/yiisoft/yii2-smarty
Now we can use ViewRenderer from Smarty or Twig.
$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
'model' => $model,
]);
But I see this error:
Unable to find template "template.twig" (looked into: .).
How can I use Smarty or Twig to render a template with the content from database in Yii2 ?
I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:
$m = new \Mustache_Engine();
echo $m->render("Hello {{model.client.firma}}",['model' => $model]);
You don't create a renderer manually, you configure your application components, like this:
[
'components' => [
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
]
You are telling Yii that there is a renderer to handle templates with the .twig extension.
Then, all you need to do is add the .twigextension when calling render in your controller actions:
public function actionIndex()
{
return $this->render('index.twig');
Then put your twig templates in the view folders where the views normally go.
Read the documentation for the twig extension: Twig Extension for Yii 2
If you want to only use twig templates, you can avoid specifying the extension (.twig) if you set the default extension:
'components' => [
'view' => [
'defaultExtension' => 'twig',
You should create a twig component which will wrap around Twig_Environment. You can add yii\twig\Extension extension if you need. Then you should use it like this:
$renderedTemplate = Yii::$app->twig->render($template, $context);
If you really need to render a template from a string variable you may implement your own Twig_LoaderInterface. There is a Twig_Loader_String, but it's deprecated and you should not use it.

How to change the language based on user preference in Yii2?

my site start with a default language(which is English) then based on user's preference i should change it. is this possible in Yii2 ? is there any widget for this
I use contentNegotiator, without assign a language to the user the language is automatically assigned by the application.
for this
In config/main.php in bootstrap section start the component
'bootstrap' => [
'log',
'contentNegotiator',
],
in component section
'components' => [
'contentNegotiator' =>[
'class' => 'yii\filters\ContentNegotiator',
'languages' => [
'en-US',
'it-IT',
'fr-FR',
],
],
],
otherwise you can change when and where you want. Is application action eg you can do in any controller you chose. this way
\Yii::$app->language = 'zh-CN';

Yii2 create user friendly URL with parameters

Hi I've to create user friendly URL but when using with parameters it's not working.
Url:
Url::to(['site/index', 'id' => 1]);
url looks like :
localhost/testApplication/frontend/web/index.php/site/index?id=1
/forntend/config/main.php
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
//'showScriptName' => false,
'rules' => [
],
],
I want a output like
localhost/testApplication/frontend/web/index.php/site/index/id/1
and after that how to access id value in controller.
'rules' => [
'site/index/id/<id:\d+>' => 'site/index'
//'site/index/<id:\d+>' => 'site/index' Better solution
//'<controller>/<action>/<id:\d+>' => '<controller>/<action>' Will be applied for all controllers and actions
],
Routing Doc.
And after in your action :
public function actionIndex($id)
{
...
}
Is really strange, for me using the parameter 'id' always show errors, I needed to change the parameter to 'user_id', but in other parts of the code I could use, really don't know why, but try to rename the parameter.