Using standalone actions
In Yii, you can define controller actions as separate classes and then connect them to your controllers. This will help you to reuse some common functionality.
For example, you can move the backend for autocomplete fields to an action and save some time by not having to write it over and over again.
Another example is that we can create all CRUD operations as separate standalone actions. We will write, create, view, and delete operations of the model and view the list operation of models.
Getting ready
- Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
- Let's create
post
table. Create migration for this using the following command:./yii migrate/create create_post_table
- Update the just-created migration's methods and list of imported classes as follows:
<?php use yii\db\Schema; use yii\db\Migration; class m150719_152435_create_post_table extends...