yii2中restful url访问配置,控制器编写 返回数据处理 [ 2.0 版本 ]
yii2 中restful url访问配置虽然官网权威指南有,但是按着配置总很多问题,
哪位网友这块使用有什么好的方法请此处分享下,谢谢。我此处遇到点问题,有知道的麻烦回复下,感谢
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
/*['class' => 'yii\rest\UrlRule', 'controller' => ['sysuser', 'news']],*/
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'api/sysuser' ,'sysuser'],
'tokens' => [
'{id}' => '<id:\\w+>'
],
/* 'except' => ['delete', 'create', 'update'],
'extraPatterns' => [
'GET test' => 'test',
], */
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
我的配置, 'enableStrictParsing' => false
,如果设置成true, 则url后面带/等符号或字母数字都会找不到,
我的问题是怎么覆盖actions,权威只能有如何去掉有如何覆盖方法,就是没写如何使我的控制器中的action来响应
请求,比如我控制器中有一个view的action,它不会响应,而是yii\rest\ViewAction
在响应,增加自己独有的action 如何编写url?如何自定义自己返回的数据内容及其格式,我使用了
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBasicAuth::className(),
HttpBearerAuth::className(),
QueryParamAuth::className(),
],
];
$behaviors['contentNegotiator'] =[
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
],
];
return $behaviors;
}
发现 C:\Users\Administrator>curl -i -H "Accept:application/json" "http://www.dapi.com/api/sysusers?access-token=100-token"
倒是可以,但有时候会返回数组,比如访问update
还有beforeAction我这边也有问题,如果打开以下代码,会直接报错,说返回内容错误什么的
public function beforeAction($event)
{
$action = $event->id;
echo $action;
if (isset($this->actions[$action])) {
$verbs = $this->actions[$action];
print_r($verbs);
} elseif (isset($this->actions['*'])) {
$verbs = $this->actions['*'];
echo $action."bbb" ;exit();
} else {
echo $action."xxx" ;exit();
return $event->isValid;
}
$verb = Yii::$app->getRequest()->getMethod();
$allowed = array_map('strtoupper', $verbs);
if (!in_array($verb, $allowed)) {
$this->setHeader(400);
echo json_encode(array('status'=>0,'error_code'=>400,'message'=>'Method not allowed'),JSON_PRETTY_PRINT);
exit;
}
// return true;
}
*/
public function actions()
{
return [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'create' => [
'class' => 'yii\rest\CreateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->createScenario,
],
'update' => [
'class' => 'yii\rest\UpdateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->updateScenario,
],
'delete' => [
'class' => 'yii\rest\DeleteAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'options' => [
'class' => 'yii\rest\OptionsAction',
],
];
}
共 3 个回答
-
['class' => 'yii\rest\UrlRule', 'controller' => 'news', 'pluralize' => false, 'extraPatterns' => [ 'GET search' => 'search', ], ],
这样就可以增加一个支持GET的actionSearch
共 3 条回复你好,增加action我这边是可以了,我代码中断点问题,至于beforaction不能用不知道为什么。想问下你清楚
这三个验证吗HttpBasicAuth::className(), HttpBearerAuth::className(), QueryParamAuth::className(),
第一个好像是要输入用户名密码来访问api 二三不清楚,第三个好像是通过参数来验证,
但是yii封装的比较好,搞得自己有点晕,我是想做成通过解析传来的加密字符串,来判断时效和用户,添加到token的判断中倒是可以,但是这是我自己的想法,感觉yii应该有做好的,否则不会让直接配置这三个验证,你知道怎么弄吗?谢谢 -
HttpBasicAuth通过如下两种方式取值
- 有一个auth属性是自定义的回调验证方法,需要接收username或password或两者,验证成功需要返回identity
- 没有auth时,一定要有username,当作accessToken通过$user->loginByAccessToken验证,
$_SERVER['PHP_AUTH_USER'],//以PHP开头,在apache下没法直接获取,前面加了HTTP_,我测试时改源码处理的 $username = $request->getAuthUser(); //$_SERVER['PHP_AUTH_PWD'] $password = $request->getAuthPassword();
HttpBearerAuth
//Authorization,在header中设定获取 $authHeader = $request->getHeaders()->get('Authorization'); $user->loginByAccessToken
QueryParamAuth
//获取get参数,默认:access-token $accessToken = $request->get($this->tokenParam); $user->loginByAccessToken
杨淇 觉得很赞
allnaked
最后登录:2015-03-06
在线时长:4小时53分
- 粉丝1
- 金钱5
- 威望0
- 积分45