BaseController.php 3.07 KB
<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\helpers\ArrayHelper;
use backend\components\filters\AccessControl;

/**
 * Site controller
 */
class BaseController extends Controller
{

    public $layout = 'template';
    
    protected $_actionParams = [];
    
    protected $_refer;

    public function init()
    {
        if (Yii::$app->user->isGuest){
            $this->redirect('/site/login');
            Yii::$app->end();
        }
        
        parent::init();
    }
    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [];
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ]);
    }
    
    public function beforeAction($action) {
        Yii::setAlias('@web', Yii::getAlias('@admin'));
        Yii::$app->setHomeUrl(Yii::getAlias('@web'));
        Yii::$app->getRequest()->setBaseUrl(Yii::getAlias('@web'));
        Yii::$app->getRequest()->setScriptUrl(Yii::getAlias('@web'));
        
        Yii::$app->language = 'zh-CN';
        
        $this->_refer = \common\lib\Referer::get();
        
        $view = $this->getView();
        $view->params['main_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][0] ? : '';
        $view->params['sub_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][1][Yii::$app->controller->action->id][0] ? : '';
        
        return parent::beforeAction($action);
    }
    
    /**
     * 单个设置视图变量
     * @param type $name
     * @param type $value
     * @return \backend\controllers\BaseController
     */
    protected function setParam($name,$value) {
        $this->_actionParams[$name] = $value;
        return $this;
    }
    
    /**
     * 批量设置视图变量
     * @param array $params
     * @return \backend\controllers\BaseController
     */
    protected function setParams(array $params) {
        $this->_actionParams = array_merge($this->_actionParams, $params);
        return $this;
    }
    
    /**
     * json返回
     */
    public function renderJson($code,$message,$data = ''){
        header('Content-type: application/json');
        $retData =  [
            'code' => (int)$code,
            'message' => $message,
            'data' => $data,
            'md5' => $data ? md5(json_encode($data)):''
        ];
        echo json_encode($retData);
        Yii::$app->end();
    }
    
    /**
     * 视图输出方法重载
     * @param type $view
     * @param type $params
     * @return type
     */
    public function render($view, $params = array()) {
        $params = array_merge($this->_actionParams, [
            '_refer'=>$this->_refer
        ], $params);
        
        return parent::render($view, $params);
    }
    
    public function afterAction($action, $result) {
        return parent::afterAction($action, $result);
    }
    
}