2

I have a problem with my Yii urlManager. I'm using the path format and want to pass multiple get variables. The url looks like that:

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

my .htaccess:

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

I tried with the urlManager, but it doesn't work with following rules:

'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>',
)

The $_GET should look like this:

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

Can someone help me?

EDIT:

It musst work for a variable nummber of get parameters.

Dinistro
  • 5,701
  • 1
  • 30
  • 38

3 Answers3

4

Keep the official guide URL Management - Using named parameters as reference.

You should write a custom rule such as:

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

In particular, if you want to have a variable number of arguments, you should append /* to the rule, something like this:

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

With such rule, you can obtain the URL as

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

and bind $_GET params as

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

Last thing to keep in mind: check always rule precedence.

carmine
  • 1,597
  • 2
  • 24
  • 33
  • I hoped it would work for Yii 2 as well, as it would make sense, but I though Yii 2 supports this multi parameters behavior by default by setting urlFormat='path' , this is how it was accomplished in Yii 2 overcome by a workaround - https://stackoverflow.com/a/38117859/3419535 – FantomX1 Aug 26 '19 at 19:38
2

You should simply try to add this url rule :

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

And with Yii you don't really need to use $_GET, you should use action parameter binding feature : http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action-parameter-binding

soju
  • 25,111
  • 3
  • 68
  • 70
  • But what is if my second parameter is test2? then he is named test and not test2. The problem is, that CGridView and other yii Components make links like that. – Dinistro Dec 06 '13 at 15:50
  • I don't think it is the same problem, you should post another question about it with more details – soju Dec 06 '13 at 16:02
  • I think this was replied even sooner than the chosen answer, even though less verbose, I think the @soju 's answer can be replied for example in here https://stackoverflow.com/questions/38115978/yii2-pretty-url-automatically-convert-everything-with-slashes-including-all-pa or with a default feature of Yii 1 , urlManager config field urlFormat='Path' – FantomX1 Aug 26 '19 at 20:25
0

Here is the solution , you can uri component use like this ; Yii::app()->uri->segment(2);

for details follow url

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

user2677802
  • 1
  • 1
  • 3