esti
(Yii2Noob)
April 24, 2015, 1:21am
1
I want a controller created that will automatically redirects user when clicked a link to its category.
for example:
example.com
A link in homepage "Today is nice day" that falls under category day.
Now when clicked to that link it opens in this way
example.com/day/today-is-a-nice-day.html
Is it possible?
rupsk1607
(Webin2015)
April 24, 2015, 12:47pm
2
dont understand properly.
when user click on category link
u want this url?
example.com/day/today-is-a-nice-day.html
esti
(Yii2Noob)
April 24, 2015, 3:26pm
3
dont understand properly.
when user click on category link
u want this url?
example.com/day/today-is-a-nice-day.html
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
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:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<cat>/<subcat>.html' => 'subCategory/subcatDetail', // create your rule
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<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:
array(
'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.
Thanks,
Saurabh Dhariwal