刚开始觉得YII2中必定有这样的配置,然后就去google了下,发明都说不可,本身去看了下,公然,框架内里直接是写死的:(源码)vendoryiisoftyii2baseController.php
defaultAction;
}
$actionMap = $this->actions();
if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id],[$id,$this]);
} elseif (pregmatch('/^[a-z0-9-]+$/',$id) && strpos($id,'--') === false && trim($id,'-') === $id) {
$methodName = 'action' . str_replace(' ','',ucwords(implode(' ',explode('-',$id))));
if (method_exists($this,$methodName)) {
$method = new ReflectionMethod($this,$methodName);
if ($method->isPublic() && $method->getName() === $methodName) {
return new InlineAction($id,$this,$methodName);
}
}
}
return null;
}
这点有点low,不外题目倒不大,这个代码很轻易领略,我们发明,着实假如在这个源码的基本上再加上一个else就可以搞定,可是照旧不提议直接改源码。
因为我们的项目用的事yii2的advanced版本,而且内里有多个项目,还要担保其他项目行使正常(也就是个此外节制器才必要行使驼峰定名的方法会见),这也轻易:
我们可以写个components处理赏罚:commoncomponentszController.php
class zController extends Controller //这里必要担任自yiibaseController
{
/**
- Author:Steven
- Desc:重写路由,处理赏罚会见节制器支持驼峰定名法
- @param string $id
- @return null|object|InlineAction
*/
public function createAction($id)
{
if ($id === '') {
$id = $this->defaultAction;
}
$actionMap = $this->actions();
if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id],$methodName);
}
}
} else {
$methodName = 'action' . $id;
if (method_exists($this,$methodName);
}
}
}
return null;
}
}
ok ,这就可以支持行使驼峰情势会见了,虽然这个的情势许多,也可以写成一个节制器,然后其余节制器担任这个节制器就行了,可是道理是一样的
假如行使?
是必要用驼峰定名情势会见的节制器中,担任下这个zController就可以了,
class QunarController extends zController
{
public $enableCsrfValidation = false;
public function behaviors()
{
$behaviors = parent::behaviors();
unset($behaviors['authenticator']);
$behaviors['corsFilter'] = [
'class' => yiifiltersCors::className(),'cors' => [ // restrict access to
'Access-Control-Request-Method' => [''],// Allow only POST and PUT methods
'Access-Control-Request-Headers' => [''],// Allow only headers 'X-Wsse'
'Access-Control-Allow-Credentials' => true,// Allow OPTIONS caching
'Access-Control-Max-Age' => 3600,// Allow the X-Pagination-Current-Page header to be exposed to the browser.
'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],],];
//设置ContentNegotiator支持JSON和XML相应名目
/$behaviors['contentNegotiator'] = [
'class' => ContentNegotiator::className(),'formats' => [
'application/xml' => Response::FORMAT_XML
]
];/
$behaviors['access'] = [
'class' => AccessControl::className(),'rules' => [
[
'ips' => ['119.254.26.*',//去哪儿IP会见白名单
'127.0.0.1','106.14.56.77','180.168.4.58' //蜘蛛及当地IP会见白名单
],'allow' => true,];
return $behaviors;
}
}
?>
示例:
}
会见的时辰url为www.test.com/getFullHotelInfo
以上这篇Yii2行使驼峰定名的情势会见节制器(实例讲授)就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程之家。
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!