Beautiful Urls With Slug

Hi guys,

I created a controller pageController to handle pages, and I make it the default controller in the config/main.php file.

In this controller I make the default action as ‘show’ which looks like that:




public function actionShow( $slug = '' ) {


		if ( empty( $slug ) ) {

			$model=Pages::model()->findByAttributes( array( 'slug' => 'accueil' ) );

		}

		else {

			$model = Pages::model()->find('slug = :slug', array(':slug' => $slug));


			if($model===null) {

				//throw new CHttpException(404,'The requested page does not exist.');

				$slug = 'not found';

			}

		}




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

			'model' => $model,

			'slug' => $slug,

		));

	}



The problem is that I can’t figure out how to pass the slug from the request to this actionShow() method.

Thanks in advance :)

Hi,

Try this


www.yourdomain.com/index.php?r=page/show&slug=yourpage

Thanks KonApaz for your reply, it’s working but I need something like:




mydomaine.com/myslug



in config amin.php

enable urlmanager, add this rule




'urlFormat'=>'path',

'showScriptName'=>false,

'rules' => array(

'<slug:\w+>'=>'page/show'

)



First check mydomain.com/index.php/myslug without ‘showScriptName’=>false,

Tell us if it works!

It works, but I have a module (admin) that doesn’t work with this method, and it works when i drop


 '<slug:\w+>'=>'page/show'

You could post it all in one :)

So, add this


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

'<slug:\w+>'=>'page/show'

It does not work :(

It shows that it didn’t found the page (404).

Post your action of module/controller and what url you want apply in this case

Here is the action (acionShow):




public function actionShow( $slug = '' ) {

		

		if ( empty( $slug ) ) {

			$model=Pages::model()->findByAttributes( array( 'slug' => 'accueil' ) );

		}

		else {

			$model = Pages::model()->find('slug = :slug', array(':slug' => $slug));


			if($model===null) {

				throw new CHttpException(404,'The requested page does not exist.');

			}

		}




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

			'model' => $model,

			'slug' => $slug,

		));

	}



And I created a module "admin" with gii.

the url that have to be is : mydomaine.com/admin to display the module and not process through the actionShow() method.

Do you want to call module/controller/action from the default controller/action with slug?

Yes, I’d love to.

With this settings I can call mydomaine.com/admin/default, and It works, but I wish I just can type mydomaine.com/admin.

Hi again,

there are two options

  1. Add in urlmanager rule ‘admin’=>‘admin/default’, (but the admin/default not seems that is a module!)

  2. In your actionShow($slug=’’) … set a condition

if $slug==‘admin’ $this->forward(‘admin/default/index’) or $this->redirect(‘admin/default/index’)

where admin=module default=controller index=action

I used the second option and now it’s working…

Thank you KonApaz very much for your time :)