Yii2 subquery in Active Record

Assuming that your models are named BaseTwitter and BaseFollower accordingly, this should work: $subQuery = BaseFollower::find()->select(‘id’); $query = BaseTwitter::find()->where([‘not in’, ‘id’, $subQuery]); $models = $query->all();

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

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

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