user1532043
user1532043

Reputation: 899

I'm trying to beautify an URL in Yii

Since I have finished my project, just unable to understand the url beautifying. Suppose this is my url:

localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=1

Where products is a controller and index is a action of that controller. catId is the parameter I am passing through the url. I need to beautify the url to

localhost/wowwaylabs/trunk/mpi_v1/this-is-india-1

where 1 is the catId I'm passing through.

Upvotes: 0

Views: 881

Answers (4)

Stu
Stu

Reputation: 4150

Assuming this-is-india is a variable or arbitrary length (i.e. category name that may vary wildy in length or syntax, as Pitchinnate suggests in the comments) then you can do this purely using url manager with no need to edit your htaccess like so:

'urlManager'=>array(
    ...
    'rules'=>array(
        '<catName:[0-9a-zA-Z_\-]+>-<catId:\d+>'=>'products/index',
        ...
    ),
    ...
),

This will take any combination of characters with a number at the end and use the number at the end as the catId, for example:

localhost/wowwaylabs/trunk/mpi_v1/this-is-india-1

Will resolve to

localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=1&catName=this-is-india

Similarly;

localhost/wowwaylabs/trunk/mpi_v1/this-is-another-title-or-category-or-whatever-999

will resolve to:

localhost/wowwaylabs/trunk/mpi_v1/index.php?r=products/index&catId=999&catName=this-is-another-title-or-category-or-whatever

Upvotes: 2

Pitchinnate
Pitchinnate

Reputation: 7556

If you are already using Yii's URL Manager (if not follow Workonphp's instructions to turn it on) try creating a rule and add it at the top of the rules that looks like this:

'<category_id:\w+>' => 'products/index',

What is this will do, is if only one parameter (i.e. category name and id) is passed in the url and it is a string/word (:\w+ specifies this) not an number (:\d+) it will default to the products controller and the index action. It will then pass $category_id as a variable into the controller. You will then need to modify that action to pull the id out of the string like so:

public function actionIndex($category_id) {
    $pieces = explode('-',$category_id);
    $cat_id = end($pieces); //actual category id seperated from name
    //...rest of code for this function
}

Upvotes: 0

Workonphp
Workonphp

Reputation: 1675

For making url beautiful, you need to add following lines of code in .htaccess file which should be in the root folder of your project:

    Options +FollowSymLinks
    IndexIgnore */*
    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

It will keep your url without ?r.

Now uncomment the following to enable URLs in path-format(in protected/config/main.php)

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

Then add 'showScriptName'=>false, in 'urlManager' of same file. It will remove index.php from the url.

For more check these links:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
http://www.sniptrichint.com/tip-of-the-day/beautiful-url-in-yii-without-index/

I think it will solve your problem.

Upvotes: 2

andy
andy

Reputation: 2409

Through htaccess :)

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$/? index.php?r=$1 [PT,L]

</IfModule>

localhost/wowwaylabs/trunk/mpi_v1/products/index/&catId=1

Upvotes: 1

Related Questions