Problem with my rules

Hi for all, iam tried to develop one app for my personal projects but in my config file, i have this rules


'rules'=>array(

			'<lang:[a-z]{2}>' => 'page/index',

			'<lang:[a-z]{2}>/<category:[a-zA-Z0-9-]+>/<slug:[a-zA-Z0-9-]+>'=> 'node/view',

				

			'<lang:[a-z]{2}>/<_m>/<_c>' => '<_m>/<_c>',

                        '<lang:[a-z]{2}>/<_m>/<_c>/<_a>*' => '<_m>/<_c>/<_a>',

                        '<lang:[a-z]{2}>/<_m>/<_a>' => '<_m>/<_a>',

				

                        '<lang:[a-z]{2}>/<_c>' => '<_c>',

		        '<lang:[a-z]{2}>/<_c>/<_a>' => '<_c>/<_a>',

				

						

								

			),

, but the rules don´t work properly…when the second is active the rest not work and when disabled this work fine…now the url www.site.com/es/node/admin not work for me…What is the error ? Thanks

The URL www.site.com/es/node/admin matches multiple rules:




'<lang:[a-z]{2}>/<category:[a-zA-Z0-9-]+>/<slug:[a-zA-Z0-9-]+>'=> 'node/view',  	

'<lang:[a-z]{2}>/<_m>/<_c>' => '<_m>/<_c>',

//...

'<lang:[a-z]{2}>/<_m>/<_a>' => '<_m>/<_a>',

//...

 '<lang:[a-z]{2}>/<_c>/<_a>' => '<_c>/<_a>',



First match wins and the request will be routed to node/view (and will probably result in a 404).

It works a bit like the following code:




$a = 1;

if ($a == 1) {

  $b = 1;

} elseif ($a == 1) {

  $b = 2;

} elseif  ($a == 1) {

  $b = 3;

}

echo $b;



It will always echo "1";

You have to define a bit more specific rules.

OK thanks, but the second rules when is active works fine, and the rest not work. in my node controller


/**

	 * Displays a particular model.

	 * @param string $slug the slug of the model to be displayed

	 */

	public function actionView($slug)

	{

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

			'model'=>$this->loadModelSlug($slug),

		));

	}

and


public function loadModelSlug($slug)

	{

		$slug_simple = explode('/', $slug);

        $last_slug = end($slug_simple);

		$model = Node::model()->findByAttributes(array('slug'=>$last_slug));

		if($model===null)

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

		return $model;

	} 

but if is disabled the second rules the rest don´t work…more specifics rules. iam not understand …i need rules with the form controller/action

Because the second rule catches all paths that matches #^[a-z]{2}/[a-zA-Z0-9]+/[a-zA-Z0-9]+$# including paths like languageCode/yourControllerId/yourActionId or languageCode/yourModuleId/yourControllerId and once a rule has been matched all other rules are ignored.

Try to change the order of rules and write more restrictive regexes:




'<lang:[a-z]{2}>/<_c:(controllerId1|controllerId2|controllerId3)>/<_a>' => '<_c>/<_a>',

'<lang:[a-z]{2}>/<category:[a-zA-Z0-9-]+>/<slug:[a-zA-Z0-9-]+>'=> 'node/view', 



or add a static fragment to the path:




'<lang:[a-z]{2}>/articles/<category:[a-zA-Z0-9-]+>/<slug:[a-zA-Z0-9-]+>'=> 'node/view',



or wite a custom URL rule that checks if the category and slug exists in the database and returns false if not. (Still can be problematic when a category has the same name as one of your controllers).

ok…its true…you are right…the second rules match and ignore the other rules…but is a problem for me…why? because a have urls with the form


'<lang:[a-z]{2}>/<_c>/<_a>' => '<_c>/<_a>'

, that not have a category or slug and the controller/action is different of node/view …for example


en/node/admin

that belongs to admin dashboard…the main purpose is to have a category and slug in my site (like a cms), but i need the others too (I think)…ok…thanks and I try to solve this…best regards and thanks for your time.

ok…solved…


'rules'=>array(

				'<lang:[a-z]{2}>' => 'page/index',

				'<lang:[a-z]{2}>/<_c:(node|category)>/<_a>' => '<_c>/<_a>',

                

				'<lang:[a-z]{2}>/<category:[a-zA-Z0-9-]+>/<slug:[a-zA-Z0-9-]+>'=> 'node/view',

				

								

				

				

				

								

			),

, that´s work for me…thanks again…