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:
Create the
cleaner
directory and add the standaloneCleanController
controller:<?php namespace app\cleaner; use Yii; use yii\filters\VerbFilter; use yii\helpers\FileHelper; use yii\web\Controller; class CleanController extends Controller { public $assetPaths = ['@app/web/assets']; public $runtimePaths = ['@runtime']; public $caches = ['cache']; public function behaviors...