Yii2 translation does not work

You Just Follow This Steps…… Step 1: In the common directory , create messages folder. Step 2: Create i18n.php file inside common/config directory with following content: <?php return [ ‘sourcePath’ => __DIR__. ‘..’ . DIRECTORY_SEPARATOR . ‘..’ . DIRECTORY_SEPARATOR . ‘..’ . DIRECTORY_SEPARATOR, ‘languages’ => [‘en-EN’, ‘ru-RU’], //Add languages to the array for the language … Read more

Yii2 require all Controller and Action to login

Place this rule in the beginning of the rules section: [ ‘allow’ => true, ‘roles’ => [‘@’], ], Omitting the actions means all actions. So your AccessControl config will be like this: public function behaviors() { return [ ‘access’ => [ ‘class’ => AccessControl::className(), ‘rules’ => [ [ ‘allow’ => true, ‘roles’ => [‘@’], ], … Read more

Log the actual SQL query using ActiveRecord with Yii2?

Method 1 With relations that return yii\db\ActiveQuery instance it’s possible to extract the raw SQL query directly in code for example with var_dump(). For example if we have user relation: /** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), [‘id’ => ‘user_id’]); } You can then var_dump() the raw SQL like that: var_dump($model->getUser()->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql); … Read more

Yii2. Access to higher level folder

Step : 1 First create .htaccess file in here yii-application1/.htaccess Options +FollowSymlinks RewriteEngine On # deal with backend first RewriteCond %{REQUEST_URI} /(backend) RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L] RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L] RewriteRule ^backend/image/(.*)$ backend/web/image/$1 [L] RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|image)/ RewriteCond %{REQUEST_URI} /(backend) RewriteRule ^.*$ backend/web/index.php [L] RewriteCond %{REQUEST_URI} /(assets|css|js|img|font) RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L] RewriteRule ^css/(.*)$ frontend/web/css/$1 [L] … Read more

LIMIT is not working in ActiveDataProvider

Here is what happens when preparing models in yii\data\ActiveDataProvider: /** * @inheritdoc */ protected function prepareModels() { if (!$this->query instanceof QueryInterface) { throw new InvalidConfigException(‘The “query” property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.’); } $query = clone $this->query; if (($pagination = $this->getPagination()) !== false) { … Read more

Yii2 disable Bootstrap Js, JQuery and CSS

In web.php config file add the following code into components array: ‘assetManager’ => [ ‘bundles’ => [ ‘yii\bootstrap\BootstrapPluginAsset’ => [ ‘js’=>[] ], ], ], To be more comprehensive: in order to disable Css (bootstrap.css): ‘assetManager’ => [ ‘bundles’ => [ ‘yii\bootstrap\BootstrapAsset’ => [ ‘css’ => [], ], ], ], in order to disable JS (bootstrap.js): … Read more

How to use events in yii2?

I can explain events by a simple example. Let’s say, you want to do few things when a user first registers to the site like: Send an email to the admin. Create a notification. you name it. You may try to call a few methods after a user object is successfully saved. Maybe like this: … Read more

Yii2 REST query

UPDATE: Apr 29 2016 This is one more approach simpler than the one I introduced in the previous update. It is always about involving the Search class generated by gii. I like using it to define and maintain all the search related logic in a single place like using custom scenarios, handle validations, or involve … Read more

How to implement Yii2 Modal Dialog on Gridview view and update button?

First of all you should add the class to the view link, not id, since there are more than one element: Change in options: ‘class’ => ‘activity-view-link’, And in JS: $(‘.activity-view-link’).click(function() { You can extract element id from corresponding parent tr. It’s stored in data-key attribute. $(‘.activity-view-link’).click(function() { var elementId = $(this).closest(‘tr’).data(‘key’); } Note that … Read more