Yii Urlmanger Rules Showing 404 Error

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

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.

category or categories?

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




trying to redirect from category page to categories controller

This is something completely different that does not come from any code pasted here so the routing worked in this case.

Also "CDbCommand" makes me wonder… are you using yii 2.0? You are in the yii 2.0 forum here…

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



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',




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

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?

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/’