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

2018-03-05  本文已影响0人  duandaoke

若存在如下控制器

class BindController extends CController {

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

在yii2中, 访问的时候, 就需要 http://your-domain-name/bind/get-mobile-phone 通过这种方式来访问

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;
    }
上一篇 下一篇

猜你喜欢

热点阅读