【问题标题】:Multiple GET-variables in yii path urlyii 路径 url 中的多个 GET 变量
【发布时间】:2013-12-23 22:48:56
【问题描述】:

我的 Yii urlManager 有问题。我正在使用路径格式并希望传递多个 get 变量。网址如下所示:

/Yii/app/de/user/admin/id/5/test/hello 

我的 .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /Yii/app/
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

我尝试使用 urlManager,但它不适用于以下规则:

'rules' => array(
    '<language:\w+>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
    '<language:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<language:\w+>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
)

$_GET 应该如下所示:

array(3) { 
    ["/de/user/admin"]=> string(0) ""
    ["id"]=> string(1) "5" 
    ["test"]=> string(5) "hello" 
    ["language"]=> string(2) "de" 
}

有人可以帮我吗?

编辑:

它必须适用于可变数量的获取参数。

【问题讨论】:

  • 您期待什么? app=de user=admin id=5 test=hello ?
  • 正如我所说:“$_GEt 应该如下所示:...”

标签: php .htaccess url yii


【解决方案1】:

保留官方指南URL Management - Using named parameters作为参考。

您应该编写一个自定义规则,例如:

'<language:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>/test/<test:\w+>'=>'<controller>/<action>'

特别是,如果您想拥有可变数量的参数,您应该将/* 附加到规则中,如下所示:

'<language:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',

通过这样的规则,您可以获取URL为

/de/user/admin/id/2/test2/hello/anotherparam/45/yap/thisothertoo

并将 $_GET 参数绑定为

'id' => 2
'test2' => 'hello'
'anotherparam' => 45
'yap' => 'thisothertoo'

要记住的最后一件事:检查总是 规则优先级。

【讨论】:

  • 我希望它也适用于 Yii 2,因为它是有意义的,但我虽然 Yii 2 通过设置 urlFormat='path' 默认支持这种多参数行为,这就是它的样子在 Yii 2 中通过解决方法完成 - stackoverflow.com/a/38117859/3419535
【解决方案2】:

您应该简单地尝试添加此 url 规则:

'<language:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>/test/<test:\w+>'=>'<controller>/<action>',

而在 Yii 中你真的不需要使用$_GET,你应该使用动作参数绑定功能:http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action-parameter-binding

【讨论】:

  • 但是如果我的第二个参数是test2呢?然后他被命名为 test 而不是 test2。问题是,CGridView 和其他 yii 组件会创建这样的链接。
  • 我不认为这是同一个问题,您应该发布另一个有关它的问题,并提供更多详细信息
  • 我认为这个回答比选择的答案更早,即使不那么冗长,我认为@soju 的答案可以在这里stackoverflow.com/questions/38115978/… 或使用 Yii 的默认功能来回答1、urlManager配置字段urlFormat='Path'
【解决方案3】:

这里是解决方案,你可以像这样使用uri组件; Yii::app()->uri->segment(2);

详情请关注网址

http://www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/

【讨论】:

  • 你的链接失效了
猜你喜欢
  • 1970-01-01
  • 2019-10-18
  • 2021-08-29
  • 2013-04-22
  • 2013-10-31
  • 2020-04-16
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
相关资源
最近更新 更多