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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!