Creating reusable controllers

In Yii, you can create reusable controllers. If you are creating a lot of applications or controllers that are of the same type, moving all common code into a reusable controller will save you a lot of time.

In this recipe, we try to create a common CleanController, which will clear temporary directories and flush cached data.

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.

How to do it…

Carry out the following steps to create reusable controllers:

  1. Create the cleaner directory and add the standalone CleanController controller:
    <?php namespace app\cleaner; use Yii; use yii\filters\VerbFilter; use ...

Get Yii2 Application Development Cookbook - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.