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 : ActiveQuery Example and what is the reason to generate ActiveQuery class separately in Gii?

The Active Query represents a DB query associated with an Active Record class. It is usually used to override the default find() method of a specific model where it will be used to generate the query before sending to DB : class OrderQuery extends ActiveQuery { public function payed() { return $this->andWhere([‘status’ => 1]); } … Read more

Multiple database connections and Yii 2.0

First you need to configure your databases like below: return [ ‘components’ => [ ‘db1’ => [ ‘class’ => ‘yii\db\Connection’, ‘dsn’ => ‘mysql:host=localhost;dbname=db1name’, //maybe other dbms such as psql,… ‘username’ => ‘db1username’, ‘password’ => ‘db1password’, ], ‘db2’ => [ ‘class’ => ‘yii\db\Connection’, ‘dsn’ => ‘mysql:host=localhost;dbname=db2name’, // Maybe other DBMS such as psql (PostgreSQL),… ‘username’ => … Read more

Disable CSRF validation for individual actions in Yii2

For the specific controller / actions you can disable CSRF validation like so: use Yii; … Yii::$app->controller->enableCsrfValidation = false; Or inside a controller: $this->enableCsrfValidation = false; Take a look at $enableCsrfValidation property of yii\web\Controller. Update: Here is some specification. If you want to disable CSRF validation for individual action(s) you need to do it in … Read more

Enable clean URL in Yii2

I got it working in yii2. Enable mod_rewrite for Apache. For basic template do the following: Create a .htaccess file in web folder and add this RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward it to index.php RewriteRule . index.php Then … Read more

Yii2 htaccess – How to hide frontend/web and backend/web COMPLETELY

Try this with .htaccess Method- Step 1 Create .htaccess file in root folder, i.e advanced/.htaccess and write below code. Options +FollowSymlinks RewriteEngine On # deal with admin first RewriteCond %{REQUEST_URI} ^/(admin) <—— RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L] RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L] RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/ <—— RewriteCond %{REQUEST_URI} ^/(admin) <—— RewriteRule ^.*$ backend/web/index.php [L] RewriteCond %{REQUEST_URI} ^/(assets|css) … Read more