blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 20, 2013,  6:34pm
               
               
          1 
           
         
        
          Trying to redirect to controller action  category/main/view
when category page is accessed
get 404 page not found
Below is the configuration and controller details
config/main.php
		'urlManager'=>array(
			'urlFormat'=>'path',
			'rules'=>array(				
			'/' => 'site/index',
			'<controller:\w+>/<id:\d+>'=>'<controller>/view',
 			'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
			'<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
			'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
  			'<module:\w+>/<controller:\w+>/<action:\w+>'=>			
                       '<module>/<controller>/<action>',
			'category/<id:\d+>' => 'categories/main/view',
			'category/<url:[-a-zA-Z0-9_+\.]{1,255}>' => 'categories/main/view',
			'producttype/<id:\d+>/*' => 'producttype/main/view',
			'producttype/<type:[a-zA-Z0-9-]+>/' =>'producttype/main/view',
                        'producttype/' => 'producttype/main/index',	
			),
			'showScriptName'=>false,
		),
controller category
class MainController extends Controller {
	public function actionView($id){
		$this->redirect(array('index'));
	}
	public function actionIndex(){
		$this->render('index');		
         }
}
controller producttype
class MainController extends Controller {
	public function actionView($id){
		$this->redirect(array('index'));
	}
	public function actionIndex(){
		$this->render('index');		
         }
}
urls
example.com/category/1/
example.com/category/cat1/
example.com/producttype/1/
example.com/producttype/prod1/
 
it successfully redirects to page when I access without query strings like
example.com/category/ 
example.com/producttype/ 
lists all the categories and products
when tried with category/producttype id or name it gives 404 error
How to write the correct rules to list the products by categories id or type id
when I tried with
example.com/producttype/?id=1  it shows the page
Below does not work when I tried with name
example.com/producttype/?type=prod1 
         
         
           
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            kartikv  
            
           
           
          
              
                December 20, 2013,  6:51pm
               
               
          2 
           
         
        
          Your controller and action params and rules seem to potentially conflict with your urlmanager rules, in the way you are using them. Can you not simplify your action definitions and URL manager rules to have unique parameters:
For example define action this way
// ProductTypeController
public function actionView($id = 0, $type = null) {
   if ($type == null) {
      // consider $id
   }
   /* do actions */
}
 
and use just the following rule in your urlmanager which should work for all scenarios:
   'producttype/<id:\d+>/<type:>' => 'producttype/main/view',
 
Depending on whichever parameter… either [font="Courier New"]id [/font] or [font="Courier New"]type[/font]  is passed, your url generated will be unique.
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 20, 2013,  7:40pm
               
               
          4 
           
         
        
          
// ProductTypeController
public function actionView($id = 0, $type = null) {
   if ($type == null) {
      // consider $id
   }
   /* do actions */
}
 
   'producttype/<id:\d+>/<type:>' => 'producttype/main/view',
 
 
 
I modified the code as above still page is redirecting 404 error when tried to access the page
example.com/producttype/id/1/ 
And I get error 500 when tried with type
example.com/producttype/?type=prod1 
Error 500
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
 
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 20, 2013,  7:53pm
               
               
          5 
           
         
        
          
 CeBe:
 
category or categories?
 
 
trying to redirect from category page to categories controller
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            CeBe  
            
           
           
          
              
                December 20, 2013,  7:58pm
               
               
          6 
           
         
        
          
And I get error 500 when tried with type
example.com/producttype/?type=prod1 
Error 500
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
 
 
 
This is something completely different that does not come from any code pasted here so the routing worked in this case.
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            CeBe  
            
           
           
          
              
                December 20, 2013,  9:00pm
               
               
          7 
           
         
        
          Also "CDbCommand" makes me wonder… are you using yii 2.0? You are in the yii 2.0 forum here…
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            kartikv  
            
           
           
          
              
                December 21, 2013,  2:36am
               
               
          8 
           
         
        
          
I see you are using Yii 1.0 as pointed out by Cebe, you may use the Yii 1 forum to get better answers. Anyway, I see a few mistakes in understanding the usage of urlmanager. Importantly also check code in your controller action as to how you are using your action parameters.
For usage of url rules, in my example earlier with my controller action example:
'producttype/<id:\d+>/<type:>' => 'producttype/main/view',
 
For id = 1, the url generated should be
example.com/producttype/1/
 
For type = prod1, the url generated should be
example.com/producttype/0/prod1
 
For both id and type passed (if you allow it in your action):
example.com/producttype/1/prod1
 
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            kartikv  
            
           
           
          
              
                December 21, 2013,  2:39am
               
               
          9 
           
         
        
          Forgot to add… you have all these rules stacked up. To test the source of the problem, remove all these rules you have below and only include the specified controller and action. Then try adding rules one by one to see which is violating.
                      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                        '<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
                        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
                        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                        '<module:\w+>/<controller:\w+>/<action:\w+>'=>                  
                       '<module>/<controller>/<action>',
                        'category/<id:\d+>' => 'categories/main/view',
                        'category/<url:[-a-zA-Z0-9_+\.]{1,255}>' => 'categories/main/view',
                        'producttype/<id:\d+>/*' => 'producttype/main/view',
 
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 21, 2013,  9:26am
               
               
          10 
           
         
        
          
It is resolved now redirecting both id and type to the respective controllers
example.com/productype/washing/ 
example.com/producttype/1/ 
I modified the code and created separate views to type and view
	public function actionView($id){
         ....
         ....
          $this->redirect(array('vuew'),array('id' => $id));  
        }
	public function actionViewByType($type){
          $this->redirect(array('vuew'),array('type' => $type));  
        }
config/main.php
                'urlManager'=>array(
                        'urlFormat'=>'path',
                        'rules'=>array(                         
			'/' => 'site/index',
			'productype/<id:\d+>' => 'productype/main/view',
			'productype/<type:[a-zA-Z0-9-]+>' => 'productype/main/viewByType',
            'listingtype/' => 'daillistingtype/main/index',
			'<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>/*' =>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',			
                        ),
                        'showScriptName'=>false,
                ),
 
And 500 Error was error in the model method which fetch the record by slug. I missed the quotes around the slug field in the where condition. I have corrected it and it is working fine.
public static function findNameBySlug($slug) {
$sql = 'SELECT id, type_name FROM product_type WHERE product_type_slug = '.$
return Yii::app()->db->createCommand($sql)->queryAll();
}
 
Thanks
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 21, 2013,  9:56am
               
               
          11 
           
         
        
          I have one more question
How to write the rules when I have categories and sub-categories like
example.com/cat1/subcat/ 
I need the full path ‘cat1/subcat/’ which I can split and get the products
how to get the paths?
         
         
        
            
            
            
         
         
             
             
          
       
      
        
          
          
            blpraveen  
            (Blpraveen2004)
           
           
          
              
                December 21, 2013, 10:06am
               
               
          12 
           
         
        
          I solved it myself
I modified the rules like
   productype/<type:[a-zA-Z0-9-\/]+>' => 'productype/main/viewByType',
 
Now I receive the path in the view ‘cat1/subcat/’