使用CamelCase操作名称时的Yii2路由 [英] Yii2 routing when using CamelCase action names
问题描述
如果您说的是以下控制器结构
If you have say the following controller structure
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
/**
* Test controller
*/
class TestController extends Controller
{
public function actionMyaction(){
...
//action logic
}
public function actionMyAction(){
...
//action logic
}
}
可以使用路径example.com/test/myaction
每个Yii 1.x逻辑的第二条路径应该可以从路径example.com/test/myAction
访问
Yii2.x中的路由使用连字符结构,并且只能从example.com/test/my-action
The second route per Yii 1.x logic should be accessible from the path example.com/test/myAction
in Yii2.x routing is using hyphenated structure and is accessible only from example.com/test/my-action
无论如何,是否最好在Yii2中使用camelCase结构启用路由,而不用扩展路由类?
Is there anyway to enable routing using camelCase structure in Yii2 preferably without extending with routing classes ?
这很重要,因为它破坏了所有链接(当然是整个Internet上)的向后兼容性,因此,即使代码被完全重写,Yii1.x应用程序也永远无法迁移到Yii2.x.发生此更改的原因是什么?
This is important as it breaks all link ( which are of course all over the internet) backward compatibility and thus Yii1.x app can never be migrated to Yii2.x even if the code is fully rewritten. What was the reason for this change?
推荐答案
我也对此变化有所了解,但最终我发现它使URL更易于阅读.我不确定在Yii1中是否有区分大小写的路由,在Yii2中我不再有此问题(或问题的印象).
I was thrown a little about this change too, but eventually I found it makes the URL easier to read. I was unsure about having a case sensitive route in Yii1, in Yii2 I do not have this problem (or impression of a problem) anymore.
我不确定确切的原因,但是我可以告诉你,对于SEO来说,最好-分开单词而不是一个大单词.
I am not sure about the exact reason, but I can tell you that for SEO it is better to have - separating words instead of having 1 big word.
当我在yii2中重写应用程序时,我将需要维护的所有旧路由都放入了url管理器中.
When I rewrote an application in yii2, I put in the url manager all the old routes that i need to maintain.
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
.................................................
'site/registerInterest' => 'site/register-interest',
.................................................
],
],
所以我的旧链接现在可以正常工作了.如果您希望从旧路由到新路由以保持SEO优势,还可以在.htaccess中放置301重定向.
So my old links work now just fine. You can also put a 301 redirect in .htaccess if you want from the old routes to the new ones to keep the SEO juice.
这篇关于使用CamelCase操作名称时的Yii2路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!