category controller

(Nirajanghimirey Com Np) #1

I want a controller created that will automatically redirects user when clicked a link to its category.

for example:

A link in homepage "Today is nice day" that falls under category day.

Now when clicked to that link it opens in this way

Is it possible?

(Webin2015) #2

dont understand properly.

when user click on category link

u want this url?

(Nirajanghimirey Com Np) #3

yes, I want my data displayed with category.

day in above url= category for the content and today-is-a-nice-day is url/slug for the content

(Saurabhdhariwal737) #4

You need to take a new field named “slug” in both the tables “Category” and “Sub-category”. Use these ‘slug’ field instead of ‘id’ field as an arguments in your view file.

Add a custom rule in app\config\main.php file as follows:




        '<cat>/<subcat>.html' => 'subCategory/subcatDetail',  // create your rule



        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',    ),


Add following column as a link of Sub-category name in app\views\subCategory\admin.php (in CgridView) to call subCategoryDetail page:


    'name' => 'title',

    'header' => 'Sub Category',

    'type' => 'raw',

    'value' => 'CHtml::link("$data->slug", Yii::app()->createUrl("subCategory/subcatDetail", array("cat" => $data->Category->slug, "subcat" => $data->slug)))',


You can get slugs of Category and Sub-category as follows from which you can fetch specific Sub-category:

public function actionSubCatDetail() {

    $cat = Yii::app()->request->getParam('cat');

    $subcat = Yii::app()->request->getParam('subcat');


Let me know if you face any further query regarding this.


Saurabh Dhariwal