Reputation: 841
I am trying to integrate url for multiple controllers/actions in different ways. Suppose I have two controllers site and product controller. I wish to get the value of correspondent controller, While pass different parameter in the url. I mean when I pass slug parameter it must fetch from site/pages and when I pass category parameter then it must fetch from product/view .
'<category:>/<id:>' => 'product/view',
'<category:>' => 'product/view',
'<slug:>' => 'site/pages',
I have tried with above example and it works fine for category but 404 error occur on slug.
EDIT
This is how my rest of task looks.
Product Controller action
public function actionView($category, $id= null)
{
$category = Category::findOne(['slug' => $category]);
// rest of work
}
And this is my createUrl params for product
Yii::$app->urlManager->createUrl(['/product/view','category'=> 'handicraft-product', 'id'=> 'nepali-thanka-134']);
And I just want to access this action like http://www.example.com/handicraft-product or http://www.example.com/handicraft-product/nepali-thanka-134
Site Controller action
public function actionPages($slug)
{
$model = $this->findByPageSlug($slug);
return $this->render('pages',['model' => $model]);
}
And this is my createUrl params for site's pages action
Yii::$app->urlManager->createUrl(['/site/pages','slug' => 'terms-and-condition']);
And this is how I want to access this action like http://www.example.com/terms-and-condition
How can I fix this problem? Thanks in advance.
Upvotes: 1
Views: 859
Reputation: 9367
2 options:
1) make the URL a little different. You could define them like this:
'category/<category:>' => 'product/view',
'page/<slug:>' => 'site/pages',
Depending on how you define the links they might actually help your SEO.
2) Create a custom URL manager rule to handle the text for both pages and categories. See here:
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#creating-rule-classes
In the custom rule you can try finding the category URL and if you cannot then default to a page URL.
Upvotes: 1