Yii2 routing when using CamelCase action names

前端 未结 2 510
悲哀的现实
悲哀的现实 2020-12-10 13:09

If you have say the following controller structure



        
2条回答
  •  温柔的废话
    2020-12-10 13:12

    You can make your own Basecontroller and overwrite createAction with a pattern allow uppercase like

    preg_match('/^[a-zA-Z0-9\\-_]

     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-zA-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;
    }
    

提交回复
热议问题