Createurl Base On Db Url Field

Hi,

I have some Articles in my database, for example :

Article

id -> 1

title -> The sun

content -> The sun is hot

url(unique) -> the-sun

Now i can view it using url example.com/article/1 or example.com/index.php/article/view/1.

I want to be able to see all my articles using the url field in the database, like :

[b][size="3"]example.com/article/the-sun

example.com/article/the-moon[/size][/b] (related to .../article/2 for example)

I guess i must use the urlManager or the createUrl function but I can’t find where to do this :confused:

Thanks in advance for your help.

Mex

Try this urlmanager


	

'urlManager'=>array(

    'urlFormat'=>'path',

	'rules'=>array(

            'article/<id:\w[a-zA-Z -0-9]+>'=>'article/view',

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

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

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

	),

),



u need to create links?

example


echo CHtml::link('my-link', array('/mailbox'),array('class'=>'my-class'));

replace array(’/mailbox’) this array with database value, prefix slashes if needed.

Thanks for replies.

I already have the dynamics links :


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

Mbala, your code will point from .../article/the-sun to .../article/view/the-sun

That’s almost it but it must point to [color="#902090"].../article/view/1[/color] (where 1 is the id of the article with url ‘the-sun’)

And I need this to be dynamic.

thanks again

use the following method to get the id of url




public function actionView($id){

    $model=Article::model()->find("url='".$id."'");

    $id=$model->id;

    ........

    ........

}



I am following this logic. If you get different one, please update here.

if u use a url like “example.com/article/the-sun” , u will get “the-sun” as $_REQUEST[‘id’] in the article/view action

mbala is right!

as $_REQUEST[‘id’] or $id

then use findByAttributes to find the match and pass that model to ur view

This works perfectly !! and I could understand how it works.

A thousand thanks to both of you ! :D

Edit:

I had to add a few lines in urlManager and actionView to make the url like

article/create article/admin etc… and

article/1

still work:


	public function actionView($id)

	{

		if (Yii::app()->user->isAdmin()){

			$this->layout='//layouts/adminColumn2';

		}

		

		if (intval($id)==0){

			$model=Article::model()->find("url='".$id."'");

	    	$id=$model->id;

		}

    	

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}


		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

		

				'article/index'=>'article/index',

				'article/admin'=>'article/admin',

				'article/delete'=>'article/delete',

				'article/create'=>'article/create',

				'article/update'=>'article/update',

				'article/<id:\w[a-zA-Z -0-9]+>'=>'article/view',

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

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

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

			),

		),




Mexos