Arif
Arif

Reputation: 1726

Rewriting a URL in Yii

I want to access a url in Yii using this url www.example.com/detail/post.

I should be able to process this url either to action detail with parameter having the value post or to some action having a named parameter detail and the value post.

I read the Yii documentation about createUrl but couldn't understand it.

Upvotes: 1

Views: 1974

Answers (2)

Fydo
Fydo

Reputation: 1424

If you have mod rewrite enabled and want to place the following in your htaccess file

php_value upload_max_filesize 1M
DirectoryIndex index.php

Options +FollowSymlinks
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

and add the following to the 'urlManager' array in your config/main.php file:

'showScriptName'=>false,

Will remove the index.php from your url and make sure to only display urls in the form of domain.com/controller/action

Upvotes: 0

Avanche
Avanche

Reputation: 1820

I think this is what you're looking for is:

update main.php to the following (change controller in the part that says controller/detail to whatever the name of your controller is)

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => array(
            'detail/<post:\d+>'=>'controller/detail',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
     ),
),

So basically you're telling Yii that any request in the format detail/number will go to your controller and use action "detail" and then pass a variable called "post".

Upvotes: 2

Related Questions