登录 网站/distributor/default/index之后,保存当前的session,想要进入 网站/company/default/index 时通过权限设置,若session有company相关的记录就跳转到相关页面,若session中没有company相关的记录就跳转到登录的页面.
yii2.0显示的代码:
代码位置:
<?php
namespace appmodulescompany;
use yii;
use yiihelpersUrl;
class company extends yiiaseModule
{
public $controllerNamespace = 'appmodulescompanycontrollers';
public function init()
{
parent::init();
// custom initialization code goes here
Yii::$app->errorHandler->errorAction = 'company/default/error';
}
public function beforeAction($action)
{
if(parent::beforeAction($action))
{
if ($action->controller->id == 'default' && in_array($action->id, array('login', 'logout'))) {
// 这里是不需要登陆验证的
if($action->id == 'login') {
$session = Yii::$app->session;
if($session['companyId'] && $session['companyName']) {
$action->controller->redirect(['default/index']);
}
}
} else {
$session = Yii::$app->session;
$session->open();
if(!$session['companyId'] || !$session['companyName']) {
$action->controller->redirect(['default/login']);
}
}
return true;
}
else
return false;
}
}