Yii2-了解urlManager

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-

我有以下可能的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-post
peru/blog/
peru/
blog/search/my-search
blog/tag/my-tag
blog/my-blog-post
blog/
/
工作原理:

据我所知,url管理有两个过程:

  • 在浏览器上写入url时。在本例中,Yii尝试将此url转换为路由和参数

  • 在示例中,当您使用Yii::$app->urlManager->createAbsoluteUrl创建url时

  • 根据这些,我正在urlManager中编写一些规则,首先是常规配置:

    '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

    问题:

  • 在我使用的规则中,我认为它与预定义的变量相冲突,如:,。我尝试过更改它,但仍然存在相同的问题

  • 就国家而言,我必须添加可能的选项:秘鲁、法国使其发挥作用,如果不起作用,没有这些选项,我如何使其发挥作用

  • url匹配取决于查询参数的数量,还是将控制器和操作也计算在内

  • 在创建url时,如何使规则的空参数被忽略

  • 为什么要将控制器添加到url

  • 规则顺序正确吗


  • 我想我找到了解决我所有问题的办法:

  • 到目前为止,我还没有找到规则的预定义关键字:

  • Yii只知道我们发送了一个参数,因此如果规则中有:

    '<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']);