<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

/**
 * 控制器基类
 */
class BaseController extends Controller
{

    /**
     * @inheritdoc
     */
/*    public function beforeAction($action)
    {
        
        $result = $this->urlAddPrefix($action);
        if (is_bool($result)) {
            return $result;
        }
        
        return parent::beforeAction($action);
    }*/
    
    /**
     * 如果URL不以前缀开头，则给URL加上前缀并跳转
     * @param \yii\base\Action $action the action to be executed.
     * @return bool|null 如果返回布尔值，则beforeAction直接返回该值，如果返回null，beforeAction不做任何处理
     */
/*    protected function urlAddPrefix($action)
    {
        $controllerId = $action->controller->id;
        $actionId = $action->id;
        if (!Yii::$app->request->isAjax && !in_array($controllerId, ['ch', 'cp'])) {
            $path = '/ch/' . $controllerId;
            if ($actionId != $action->controller->defaultAction) {
                $path .= '/' . $actionId;
            }

            $url = rtrim(Yii::$app->request->url, '/');

            // 以/ch/开头，但是经过urlManage解析，得到的controller的id不是ch
            if ($url === '/ch' || strpos($url, '/ch/') === 0) {
                return true;
            }

            // 后缀
            $suffix = '.html';
            if (strrpos($url, $suffix) === strlen($url) - strlen($suffix)) {
                $url = strstr($url, $suffix, true);
            }

            // 文章列表
            if (preg_match('/^(\/ch)?\/a_(\d+)/', $url)) {
                $path = strpos($url, '/ch') === 0 ? $url : '/ch' . $url;
            }

            // 文章详情
            if (preg_match('/^(\/ch)?\/ad_(\d+)/', $url)) {
                $path = strpos($url, '/ch') === 0 ? $url : '/ch' . $url;
                $path .= $suffix; // 详情页要加上后缀
            }

            $this->redirect($path);
            return false;
        }
    }*/

}