查看“Yii2”的源代码
←
Yii2
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
== 认证方式配置 == 参考教程: https://www.yiichina.com/tutorial/812 <nowiki> public function behaviors() { $behaviors = parent::behaviors(); $behaviors['authenticator'] = [ //auth method 1 //'class' => HttpBasicAuth::className(), //auth method 2 'class' => HttpBearerAuth::className(), //auth method 3 //'class' => QueryParamAuth::className(), //auth method 4 复合认证 'class' => CompositeAuth::className(), 'authMethods' => [ HttpBasicAuth::className(), HttpBearerAuth::className(), QueryParamAuth::className(), ], 'optional'=>['login'],// 不用认证的action, 配置的是action id。 ]; return $behaviors; } </nowiki> identityClass继承IdentityInterface,实现findIdentityByAccessToken等方法 == yii2自定义异常页面 == 框架中异常是由yii\web\ErrorHandler类处理,所以继承他并重写(renderException)就好了。 1. 创建类app\components\ErrorHandler <nowiki> +<?php +/** + * @link http://www.yiiframework.com/ + * @copyright Copyright (c) 2008 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +namespace app\components; + +use Yii; +use yii\base\ErrorException; +use yii\base\Exception; +use yii\web\ErrorHandler as BaseErrorHandler; +use yii\helpers\VarDumper; +use \yii\web\Response; + +class ErrorHandler extends BaseErrorHandler +{ + protected function renderException($exception) + { + if ($exception instanceof \yii\web\UnauthorizedHttpException) { + $response = new Response(); + $response->format = Response::FORMAT_JSON; + $response->data = ["status"=>1001,"msg"=>"未登录"]; + $response->send(); + return; + }else{ + return parent::renderException($exception); + } + } +} </nowiki> 2. 修改config/main.php <nowiki> 'components'=>[ + 'errorHandler'=>[ + 'errorAction' => 'site/error', + 'class' => 'app\components\ErrorHandler', + ], ], </nowiki> 当抛出yii\web\UnauthorizedHttpException异常时,就会返回上面定义的<code>{"status": 1001,"msg": "未登录"}</code>了
返回至
Yii2
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息