gpt4 book ai didi

php - Yii 没有检测到 Camel 案例 Action

转载 作者:行者123 更新时间:2023-12-01 12:32:46 26 4
gpt4 key购买 nike

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

网站 Controller .php

public function actionRegisterUser()

这就是我在 main.php

中的调用方式
 ['label' => 'Register User', 'url' => ['/site/RegisterUser']],

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

 public function actionRegisteruser

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

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

我也尝试使用 Controller 的 rules,但没有解决任何问题。

最佳答案

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

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

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

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

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

[
// ...other rules...

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

关于php - Yii 没有检测到 Camel 案例 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com