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

Yii2 redirect to previous page

You could use Yii::$app->request->referrer which returns the last page the user was on. Usage is straightforward: return $this->redirect(Yii::$app->request->referrer); You need also take into account that referrer can be null: return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl); See the docs.

How to pass param from controller to layout in YII2

yii\base\View has special $params property. For example it’s used for building breadcrumbs in default generated CRUD code templates with Gii. You can set it like this before rendering: use Yii; Yii::$app->view->params[‘customParam’] = ‘customValue’; Inside a controller you can set it like this: $this->view->params[‘customParam’] = ‘customValue’; Then it will be available in views (including main layout): … Read more

Performing raw SQL queries in Yii2?

You can execute raw sql like this $connection = Yii::$app->getDb(); $command = $connection->createCommand(” SELECT SUM(bets.balance_return) AS total_win , bets.user_id , users.user_name , users.user_status FROM bets INNER JOIN users ON bets.user_id = users.id WHERE users.user_status=”verified” AND bets.date_time > :start_date GROUP BY bets.user_id ORDER BY total_win DESC”, [‘:start_date’ => ‘1970-01-01’]); $result = $command->queryAll(); I recommend reading: http://www.yiiframework.com/doc-2.0/yii-db-connection.html#createCommand()-detail … 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

How to use not equal to inside a Yii2 query

In this case you need to use operator format: [operator, operand1, operand2, …]. So your code should look like this: $details = MovieShows::find() ->where([‘movie_id’=>$id]) ->andWhere([‘location_id’=>$loc_id]) ->andWhere([‘<>’,’cancel_date’, $date]) ->all(); More about using where method and operator format