dpmfur2635 2018-07-22 15:40
浏览 91
已采纳

Yii2:如何使用UrlManager的分页构建正确的模式?

I have following conditions:

1) expected request is /a1,a2,aN[/.../n1,n2,nN][?range=xxx-yyyy[&search=string]] (square brackets contain optional parts)

2) action method signature is public function actionIndex(string $alias = '', string $range = '', string $search = ''): string

3) so I used a rule for this:

[
    'pattern'      => '<alias:[\\w-,\\/]+>',
    'route'        => 'shop/products/index',
    'encodeParams' => false,
],

It works properly until I try to add pagination, LinkPager ignores a rule I wrote:

[
    'pattern'      => '<alias:[\\w-,\\/]+>/<page:\d+>',
    'route'        => 'shop/products/index',
    'encodeParams' => false,
],

and displays the alias and page params as GET variables.

What is a right rule adding the page number in the end of request URI like /a1,a2,aN/n1,n2,nN/2 and ignoring if the number is 1?

UPD: I found a reason, this is a rule I defined before:

'/shop' => 'shop/products/index', //it breaks following rules
[
    'pattern'      => '<alias:[\\w-,\\/]+>/<page:\d+>',
    'route'        => 'shop/products/index',
    'encodeParams' => false,
],
[
    'pattern'      => '<alias:[\\w-,\\/]+>',
    'route'        => 'shop/products/index',
    'encodeParams' => false,
],

So, how I to make all these rules work together?

  • 写回答

1条回答 默认 最新

  • douyimiao1993 2018-07-22 16:08
    关注

    Solution 1: To make another action method which works without alias argument and calls actionIndex with empty one.

    Solution 2: To make same rules with different mode in special order:

    [
        'name'         => 'This rule is first when we create a link',
        'pattern'      => '<alias:[\\w-,\\/]+>/<page:\d+>',
        'route'        => 'shop/products/index',
        'encodeParams' => false,
        'mode'         => \yii\web\UrlRule::CREATION_ONLY,
    ],
    [
        'name'         => 'This rule is first when we parse a request',
        //
        'pattern'      => 'shop/<page:\d+>',
        'route'        => 'shop/products/index',
    ],
    [
        'name'         => 'Used for parsing when previous rule does not match',
        'pattern'      => '<alias:[\\w-,\\/]+>/<page:\d+>',
        'route'        => 'shop/products/index',
        'encodeParams' => false,
        'mode'         => \yii\web\UrlRule::PARSING_ONLY,
    ],
    
    [
        'name'         => 'Same as first but when link has no page number',
        'pattern'      => '<alias:[\\w-,\\/]+>',
        'route'        => 'shop/products/index',
        'encodeParams' => false,
        'mode'         => \yii\web\UrlRule::CREATION_ONLY,
    ],
    [
        'name'         => 'First when parsing request with no page number',
        'pattern'      => 'shop',
        'route'        => 'shop/products/index',
    ],
    [
        'name'         => 'Used for parsing when previous rule does not match',
        'pattern'      => '<alias:[\\w-,\\/]+>',
        'route'        => 'shop/products/index',
        'encodeParams' => false,
        'mode'         => \yii\web\UrlRule::PARSING_ONLY,
    ],
    

    If you know a better solution with one action I'll be glad to see it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器