Url Management

Hello,

how can I simply enable pretty url for all requests?

Currently I need to call something like this:

server/index.php?r=controller/action

But I like:

server/controller/action/param1/value1/parem2/value2

What is the easiest way to handle this?

Thanks,

Urkman

I found something, but it does not cover all my needs:




        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false

        ]



with this I can call:

server/controller/action?param1=value

But still not:

server/controller/action/param1/value

Greetings,

Urkman

You need to setup url manager regex parsing rules. For example:




'urlManager' => [

  'enablePrettyUrl' => true,

  'showScriptName' => false,

  'rules' => [

    '<controller>/<action>/param1/<param1:\w+>' => '<controller>/<action>',

    // another way below

    '<controller>/<action>/p1/<p1:\w+>/p2/<p2:\w+>' => '<controller>/<action>',

  ]

]



Thanks for your answer, but this is not working for me :(

I currently always get an error, when I try to add any param/value pair:

server/controller/action?id=100

gives me the error:

Unknown option: --id

:(

Any idea?

Thank,

Urkman

For above case, Your URL rule should be something like below




'rules' => [

    // simpler use

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

    // if you want the word 'id' in between

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

]



But shouldn’t a call like this be standard functionality with our any rules?

server/controller/action?id=100 (id could be any other key)

Or what is the standard to add key/value pairs as parameters to a url?

You need to create an httaccess file in your root directory for ridirecting the request:

RewriteEngine on

If a directory or a file exists, use the request directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

Otherwise forward the request to index.php

RewriteRule . index.php

I’ve done this already :(

Did not get you. Adding this line I mentioned will work globally across for any controller and action:




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



So ‘user/update?id=200’ will become ‘user/update/200’ and so on.

Do you mean you want the word id to change to anything else?

Hi,

thanks again for your answer…

The problem here is, that ‘id’ is not a fixed parameter…

it could be everything… like ‘foo’, ‘bar’, ‘id’, ‘name’… or anything else…

Like this:

‘user/update?id=200’ or ‘user/update?foo=200’ or ‘user/update?name=urkman’

And perhaps a combination of this:

‘user/update?foo=200&bar=300’

So I need for this call:

‘user/update/id/200’ or ‘user/update/foo/200’ or ‘user/update/name/urkman’

Hope this description is better now :)

Thanks,

Urkman