yii2 控制器里 action 大小写组合造成的路由问题

yii1中, 若存在如下控制器

class BindController extends CController {

    public function actionGetMobilePhone () {
        // some code...
    }

}

那么, 通过访问 http://your-domain-name/bind/getMobilePhone  就可以访问到

以上代码如果在yii2中, 访问的时候, 就需要 http://your-domain-name/bind/get-mobile-phone 通过这种方式来访问, 因为yii2中改写了 createAction 方法

yii1中的 createAction 

    public function createAction($actionID)
    {
        if($actionID==='')
            $actionID=$this->defaultAction;
        if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method
            return new CInlineAction($this,$actionID);
        else
        {
            $action=$this->createActionFromMap($this->actions(),$actionID,$actionID);
            if($action!==null && !method_exists($action,'run'))
                throw new CException(Yii::t('yii', 'Action class {class} must implement the "run" method.', array('{class}'=>get_class($action))));
            return $action;
        }
    }

yii2中的 createAction

    public function createAction($id)
    {
        if ($id === '') {
            $id = $this->defaultAction;
        }

        $actionMap = $this->actions();
        if (isset($actionMap[$id])) {
            return Yii::createObject($actionMap[$id], [$id, $this]);
        } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $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;
    }

红色的注释就是yii1 与 yii2 中组合方法名的区别,  yii1中只是简单的组合.

转载于:https://www.cnblogs.com/debmzhang/p/5002641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值