Yii2-了解urlManager
我有以下可能的URL: 一开始就有国家:Yii2-了解urlManager,yii2,yii2-basic-app,yii2-urlmanager,Yii2,Yii2 Basic App,Yii2 Urlmanager,我有以下可能的URL: 一开始就有国家: peru/blog/search/my-search peru/blog/tag/my-tag peru/blog/my-blog-post peru/blog/ peru/ blog/search/my-search blog/tag/my-tag blog/my-blog-post blog/ / 一开始没有国家: peru/blog/search/my-search peru/blog/tag/my-tag peru/blog/my-blog-
peru/blog/search/my-search
peru/blog/tag/my-tag
peru/blog/my-blog-post
peru/blog/
peru/
blog/search/my-search
blog/tag/my-tag
blog/my-blog-post
blog/
/
一开始没有国家:
peru/blog/search/my-search
peru/blog/tag/my-tag
peru/blog/my-blog-post
peru/blog/
peru/
blog/search/my-search
blog/tag/my-tag
blog/my-blog-post
blog/
/
工作原理:
据我所知,url管理有两个过程:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => []
]
现在规则是:
'<country:peru|france>/<module:[\w\-]+>/tag/<tag:[\w\-]*>' => '<module>/index',
'<country:peru|france>/<module:[\w\-]+>/search/<search:[\w\-]*>' => '<module>/index',
'<country:peru|france>/<module:[\w\-]+>/<slug:[\w\-]*>' => '<module>/index',
'<country:peru|france>/<module:[\w\-]+>' => '<module>/index',
'<module:[\w\-]+>/tag/<tag:[\w\-]*>' => '<module>/index',
'<module:[\w\-]+>/search/<search:[\w\-]*>' => '<module>/index',
'<module:[\w\-]+>/<slug:[\w\-]*>' => '<module>/index',
'<module:[\w\-]+>' => '<module>/index',
'//tag/'=>'/index',
“//搜索/”=>“/索引”,
“//”=>“/索引”,
“/”=>“/索引”,
“/tag/”=>“/index”,
“/search/”=>“/index”,
“/”=>“/索引”,
''=>''/index',
正如您从规则中注意到的,我在url中传递了一个参数“module”,我希望该参数用作控制器
在国家队的情况下,我必须添加一些可能的匹配项,如果不是,我就无法使其工作
根据上述规则,当我在浏览器上输入“漂亮”url时,它会起作用,如:
但如果尝试创建url,我的问题就开始了:
Yii::$app->urlManager->createAbsoluteUrl(["blog/index", "module" => "blog"]
Rule: '<module:[\w\-]+>' => '<module>/index'
Url Expected: http://example.com/blog
Url Generated: http://example.com/blog?module=blog
Yii::$app->urlManager->createAbsoluteUrl(['blog/index', 'module' => 'blog', 'slug' => 'my-post'])
Rule: '<module:[\w\-]+>/<slug:[\w\-]*>' => '<module>/index'
Url Expected: http://example.com/blog/my-post
Url Generated: http://example.com/blog/my-post?module=blog
Yii::$app->urlManager->createAbsoluteUrl([“blog/index”,“module”=>“blog”]
规则:“”=>“/索引”
预期的Url:http://example.com/blog
生成的Url:http://example.com/blog?module=blog
这似乎不符合规则,不确定
如果我尝试创建url:
Yii::$app->urlManager->createAbsoluteUrl(["blog/index", "module" => "blog"]
Rule: '<module:[\w\-]+>' => '<module>/index'
Url Expected: http://example.com/blog
Url Generated: http://example.com/blog?module=blog
Yii::$app->urlManager->createAbsoluteUrl(['blog/index', 'module' => 'blog', 'slug' => 'my-post'])
Rule: '<module:[\w\-]+>/<slug:[\w\-]*>' => '<module>/index'
Url Expected: http://example.com/blog/my-post
Url Generated: http://example.com/blog/my-post?module=blog
Yii::$app->urlManager->createAbsoluteUrl(['blog/index','module'=>'blog','slug'=>'mypost']))
规则:“/”=>“/索引”
预期的Url:http://example.com/blog/my-post
生成的Url:http://example.com/blog/my-post?module=blog
从这两种情况中,我注意到它正在将控制器添加到url
问题:
我想我找到了解决我所有问题的办法:
,
'<language:[\w\-]*>' => 'language/index'
'<country:[\w\-]*>' => 'country/index'
在本例中,如果我们将“秘鲁”作为值,它将不匹配第一条规则,所以它将使用第二条规则
'' => 'site/index'
'<base:es|en>/<module:ideas|user|blog>/tag/<tag:[\w\-]*>' => '<module>/index',
'<base:es|en>/<module:ideas|user|blog>/search/<search:[\w\-]*>' => '<module>/index',
'<base:es|en>/<module:ideas|user|blog>/<slug:[\w\-]*>' => '<module>/index',
'<base:es|en>/<module:ideas|user|blog>' => '<module>/index',
'<base:es|en>/<slug:contact>' => 'site/index',
'<base:es|en>' => 'site/index',
'<module:ideas|user|blog>/tag/<tag:[\w\-]*>' => '<module>/index',
'<module:ideas|user|blog>/search/<search:[\w\-]*>' => '<module>/index',
'<module:ideas|user|blog>/<slug:[\w\-]*>' => '<module>/index',
'<module:ideas|user|blog>' => '<module>/index',
'<slug:contact>' => 'site/index',
'' => 'site/index',
'//tag/'=>'/index',
“//搜索/”=>“/索引”,
“//”=>“/索引”,
“/”=>“/索引”,
“/”=>“站点/索引”,
''=>'站点/索引',
“/tag/”=>“/index”,
“/search/”=>“/index”,
“/”=>“/索引”,
''=>''/index',
''=>'站点/索引',
''=>'站点/索引',
我希望这能为将来的人节省时间。问题在于如何创建URL。如果路由模式(规则数组中的值)中有
,则无需手动传递。它将从路由中检测
Sou您应该创建如下URL:
Yii::$app->urlManager->createAbsoluteUrl(['blog/index', 'slug' => 'my-post'])
Yii::$app->urlManager->createAbsoluteUrl(['blog/index']);