Yii 没有检测到驼峰事件 [英] Yii not detecting camel case actions

查看:44
本文介绍了Yii 没有检测到驼峰事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明这样的操作,Yii 会给我 404 错误:

SiteController.php

公共函数 actionRegisterUser()

这就是我在 main.php

中的调用方式

 ['label' =>'注册用户', 'url' =>['/site/RegisterUser']],

<小时>

我尝试了几种不同的组合.唯一可行的组合是这两个地方的命名约定:

 公共函数 actionRegisteruser'网址' =>['/网站/注册用户']

我曾经在另一个 Yii 项目(Yii 1.0)上工作,我可以用驼峰命名我的动作并毫无问题地调用它们.是否需要开启某种设置才能执行此操作?

我也尝试过使用控制器的规则,但没有解决任何问题.

解决方案

在某些情况下,您需要驼峰式链接.例如,出于 SEO 目的(保留入站链接).您可以在 Web 服务器端创建重写规则或向应用端的 URL 管理器添加内联规则.示例:

'urlManager' =>['规则' =>['/'='register-user/',],],

也可以编写自定义 URL 规则.示例:

namespace app\components;使用 yii\web\UrlRuleInterface;使用 yii\base\Object;类 CarUrlRule extends Object 实现 UrlRuleInterface{公共函数 createUrl($manager, $route, $params){如果($route === '汽车/索引'){如果 (isset($params['manufacturer'], $params['model'])) {返回 $params['manufacturer'] .'/' .$params['模型'];} elseif (isset($params['manufacturer'])) {返回 $params['制造商'];}}返回假;//这条规则不适用}公共函数 parseRequest($manager, $request){$pathInfo = $request->getPathInfo();if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) {//检查 $matches[1] 和 $matches[3] 以查看//如果它们匹配数据库中的制造商和型号//如果是,设置 $params['manufacturer'] 和/或 $params['model']//并返回 ['car/index', $params]}返回假;//这条规则不适用}}

并在 [[yii\web\UrlManager::rules]] 配置中使用新的规则类:

<预><代码>[//...其他规则...['类' =>'app\components\CarUrlRule',//...配置其他属性...],]

Yii is giving me 404 Error if I declare an action like this:

SiteController.php

public function actionRegisterUser()

This is how I call it in the main.php

 ['label' => 'Register User', 'url' => ['/site/RegisterUser']],


I tried several different combinations. The only combination that will work is this naming convention in both places:

 public function actionRegisteruser

 'url' => ['/site/registeruser']

I used to work on another Yii project (Yii 1.0) and I could name my actions in camel case and call them without any problem. Do I need to turn on some sort of setting to do this?

I also tried playing with the rules of the Controller but that didn't solve anything.

解决方案

In some cases you need camelcase link. For example, for SEO purposes (keep inbound links). You could create rewrite rule on web server side or add inline rule to URL manager on app side. Example:

'urlManager' => [
    'rules' => [
        '<controller:RegisterUser>/<action:\w+>'=>'register-user/<action>',
    ],
],

Also it's possible to write custom URL rule. Example:

namespace app\components;

use yii\web\UrlRuleInterface;
use yii\base\Object;

class CarUrlRule extends Object implements UrlRuleInterface
{

    public function createUrl($manager, $route, $params)
    {
        if ($route === 'car/index') {
            if (isset($params['manufacturer'], $params['model'])) {
                return $params['manufacturer'] . '/' . $params['model'];
            } elseif (isset($params['manufacturer'])) {
                return $params['manufacturer'];
            }
        }
        return false;  // this rule does not apply
    }

    public function parseRequest($manager, $request)
    {
        $pathInfo = $request->getPathInfo();
        if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) {
            // check $matches[1] and $matches[3] to see
            // if they match a manufacturer and a model in the database
            // If so, set $params['manufacturer'] and/or $params['model']
            // and return ['car/index', $params]
        }
        return false;  // this rule does not apply
    }
}

And use the new rule class in the [[yii\web\UrlManager::rules]] configuration:

[
    // ...other rules...

    [
        'class' => 'app\components\CarUrlRule', 
        // ...configure other properties...
    ],
]

这篇关于Yii 没有检测到驼峰事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆