Php 如何在Yii2中打印beforeAction中的文本
如何在操作前打印出函数中的数据?我想在控制器中的每个操作之前进行一些验证,因此,如果在beforeAction中出现某些情况,我应该打印数据并阻止进一步执行,例如,JSON:Php 如何在Yii2中打印beforeAction中的文本,php,yii2,Php,Yii2,如何在操作前打印出函数中的数据?我想在控制器中的每个操作之前进行一些验证,因此,如果在beforeAction中出现某些情况,我应该打印数据并阻止进一步执行,例如,JSON: [ status: "error", msg: "access denied" ] 我甚至尝试将内部重定向到另一个控制器,但它不起作用 public function beforeAction($action) { $request = Yii::$app->request; if (
[
status: "error",
msg: "access denied"
]
我甚至尝试将内部重定向到另一个控制器,但它不起作用
public function beforeAction($action)
{
$request = Yii::$app->request;
if ( ! checkByToken($request->get('token')) && $this->getRoute() != 'web/abonent/token_error') {
\Yii::$app->runAction('web/abonent/token_error');
return true;
}
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
但也许还有另一个这样做的概念。我只需要在执行任何操作之前检查条件,然后打印结果或让操作执行。我认为这样会更好
public function beforeAction($action)
{
$request = Yii::$app->request;
if ( ! checkByToken($request->get('token')) && $this->getRoute() != 'web/abonent/token_error') {
$action = 'error';
}
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
操作名称必须是“actionError”我认为这样会更好
public function beforeAction($action)
{
$request = Yii::$app->request;
if ( ! checkByToken($request->get('token')) && $this->getRoute() != 'web/abonent/token_error') {
$action = 'error';
}
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
操作名称必须为“actionError”以防止进一步执行:
public function beforeAction($action) {
return false; // key point
}
要在操作前打印数据,请执行以下操作:
public function beforeAction($action) {
// set response format = json:
Yii::$app->response->format = Response::FORMAT_JSON;
// then, set the response data:
Yii::$app->response->data = [
'status' => 'error',
'msg' => 'access denied'
];
return false;
}
为防止进一步执行:
public function beforeAction($action) {
return false; // key point
}
要在操作前打印数据,请执行以下操作:
public function beforeAction($action) {
// set response format = json:
Yii::$app->response->format = Response::FORMAT_JSON;
// then, set the response data:
Yii::$app->response->data = [
'status' => 'error',
'msg' => 'access denied'
];
return false;
}
您的方法看起来很容易使用,但在我的Yii2版本2.0.12中它不起作用。它只是打印出控制器的动作,而不是“错误”动作。您的方法看起来很容易使用,但在我的Yii2 2.0.12版中它不起作用。它只是打印出控制器的动作,而不是“错误”动作。