Yii Framework Forum: Problem with my rules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with my rules Rate Topic: -----

#1 User is offline   Yendris 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-December 14

Posted 04 July 2015 - 10:50 AM

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
0

#2 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 668
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 04 July 2015 - 11:23 AM

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.
0

#3 User is offline   Yendris 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-December 14

Posted 04 July 2015 - 01:28 PM

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
0

#4 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 668
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 05 July 2015 - 09:09 AM

View PostYendris, on 04 July 2015 - 01:28 PM, said:

the second rules when is active works fine, and the rest not work

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.

View PostYendris, on 04 July 2015 - 01:28 PM, said:

more specifics rules. iam not understand

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

#5 User is offline   Yendris 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-December 14

Posted 07 July 2015 - 09:24 AM

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.
0

#6 User is offline   Yendris 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 09-December 14

Posted 07 July 2015 - 09:36 AM

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....
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users