Urlmanager Show Title Instead Of Id

Hello Yii!

I’ve been trying to make my url more clean. What I have is:



site/projects/projects/update/id/3.html when i am updating…

I need to show the title of my project, not the id. Like that, only showing the module and the title:


This is my actual code:











I tried many things like changing ‘d’ for ‘w’ and also ‘id’ for ‘name’ as i read in others posts, but nothing works.

This are other things that i tried:


    	'<_c:(projects)>/<_a:(create|update|delete)>/<id:\d+>' => '<_c>/<_a>',

    	'<_c:(projects)>/view/' => '<_c>/<_a>',

    	'<_c:(projects)>/<id:\d+>' => '<_c>/read',

    	'<_c:(projects)>s' => '<_c>/list',


//also using modules rules, i have this site/projects/projects/id/3.html:





I am lose, what can i do?

Thanks everyone for your time…!!!

Is your title guaranteed to be unique? If not, you have a problem. Also, is it allowed be be modified once it has been created? If not, bookmarks to your pages might break.

One solution I know how to do it is: Generate URLs in a certain format that contains both the id and the title. When parsing a requested URL, you only extract the id from it and load your resources based on the id. So basically it is not important if someone requests "myapp/123-valid-title" or "myapp/123-invalid-title", because data will always be loaded based on the id "123", everything behind it is only a hint to humans.

Unfortunately, I’m not very experienced with the url manager, but I’ll give it a try. Maybe someone else can correct it if I’m doing it plain wrong. ;)

// basic url

// index.php?r=projects/view&id=123&title=some-title

// how we want to represent it

// projects/123-some-title

// required rule

// 'projects/<id:\d+>-<title:[\w-]+>' => 'projects/view'

You have to use "Custom URL Rule Class" if the title is a data that has to be looked up in a db table.



I agree with Ben. I think it’s better to include ‘id’ integer in the url. It’s much easier and more robust, and probably as good as the one without ‘id’ in SEO point of view. :)


I originally have that:


I try what you said Ben, but it doesnt work.

I used:


That shows:


I think it should be something like this:


but it doesnt show the title. Why do i use ‘title’? i try ‘name’ also…and nothing.

Where am i wrong?

I read what softark, but I didnt understand this way.


As far as I understand the UrlManager, it requires you to provide it the necessary data. So if you want to use an url rule like:


Then you also need to create URLs like this:


  'id' => $projectId,

  'title' => $projectTitle


// or to make a link:

echo CHtml::link( $linkText, array(


  'id' => $projectId,

  'title' => $projectTitle


I’m not sure how sensitive the whole thing is regarding trailing slashes. URL Management in the docu doesn’t use any, so I would stick with it.

Also a quick question, no offence intended, but do you realize what those "\d", "\d+", "\w" things in the url rules mean? I ask because you wrote:


Do you know about regular expressions?


I just have to modify my way of creating url: CHtml::link.

I use:


and change project ‘_view.php’:

//i had that

<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

//change for:

<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id, 'title'=>$data->title)); ?>

Thanks everyone for you help.

PD: I was reading Regular Expression also, but my brain is to busy today.