Creating reusable controller actions
Common actions such as deleting the AR model by the primary key or getting data for AJAX autocomplete could be moved into reusable controller actions and later attached to controllers as needed.
In this recipe, we will create a reusable delete action that will delete the specified AR model by its primary key.
Getting ready
Create a new
yii2-app-basic
application using the composer as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.Create a new database and configure it.
Create and apply the following migration:
<?php use yii\db\Migration; class m160308_093233_create_post_table extends Migration { public function up() { $this->createTable('{{%post}}', [ 'id' => $this->primaryKey(), 'title' => $this->string()->notNull(), 'text' => $this->text()->notNull(), ]); } public function down() { $this->...