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/id/3.html

or

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:

site/projects/title.html

This is my actual code:




'urlManager'=>array(

      'urlFormat'=>'path',

      'showScriptName'=>false,

      'urlSuffix'=>'.html',

      'rules'=>array(

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

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

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

			),

		),



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:




 'rules'=>array(

    	'<_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:


'rules'=>array(

      '<module:\w+>/<controller:\w+>/view/<id:\w+>'=>'<module>/<controller>/<title:\w+>', 

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

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






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.

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes

[EDIT]

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. :)

Hi,

I originally have that:

site/index.php?r=projects/projects/view&id=3

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

I used:


'projects/view/<id:\d+>'=>'projects/projects/view/'

That shows:

projects/view/3.html

I think it should be something like this:


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

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.

Thanks!

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:




'projects/view/<id:\d+>-<title:.+>'=>'projects/projects/view'



Then you also need to create URLs like this:




$this->createUrl('projects/projects/view',array(

  'id' => $projectId,

  'title' => $projectTitle

));


// or to make a link:

echo CHtml::link( $linkText, array(

  'projects/projects/view',

  '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:

and

Do you know about regular expressions?

Solved.

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

I use:


'projects/<id>/<title>'=>'projects/projects/view/'



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.