0
votes

I am trying to configure Yii2 url manager in a manner that if a controller name is skipped in url it should call the default controller for action. I have managed to achieve this without action parameter. But got stuck when using parameters in action name.

Here is my route config:

return [
    'catalog/category/<alias:[\w-]+>' => 'catalog/default/category',
    'catalog/<action:\w+>' => 'catalog/default/<action>',
];

Controller File:

namespace app\modules\catalog\controllers;

use yii\base\Controller;
use app\modules\catalog\models\Categories;

class DefaultController extends Controller
{
    public function actionShopbydepartment()
    {
        $data['categories'] = Categories::findParentSubHierarchy();
        return $this->renderPartial('shopbydepartment', $data);
    }

    public function actionCategory($alias = null)
    {
        die(var_dump($alias));
        $data['category'] = Categories::findCategoryBySlug($alias);
        return $this->render('category', $data);
    }
} 

when I access the following url it loads perfectly. http://domain.com/index.php/catalog/shopbydepartment

But when i access the below url it called the right function but did not pass the $alias value: http://domain.com/index.php/catalog/category/appliances

UPDATE:

I have used the following approach for module wise url rules declaration: https://stackoverflow.com/a/27959286/1232366

Here is what i have in the main config file:

'rules' => [
            [
                'pattern' => 'admin/<controller:\w+>/<action:[\w-]+>/<id:\d+>',
                'route' => 'admin/<controller>/<action>'
            ],
            [
                'pattern' => 'admin/<module:\w+>/<controller:\w+>/<action:[\w-]+>/<id:\d+>',
                'route' => 'admin/<module>/<controller>/<action>'
            ],
        ],

the admin is working fine and this is my first module so rest of the rules are mentioned already

2
Do you have other rules ? Show us your urlManager config. - soju
Have you tried changing the regular expression from [\w-]+ to something else. For eg. \w+ or just use <alias> without any expression. - Hanafi
@soju see my updated question - Code Prank
@Hanafi yes i have tried <alias:\w+>, <alias:.*>, <alias> - Code Prank

2 Answers

0
votes

Well just to help other fellows I have retrieve the value of $alias using the following approach:

$alias = \Yii::$app->request->get('alias');

But definitely this is not an accurate answer of the question. I still didn't know what i am doing wrong that i didn't get the value using the approach mentioned in question.

0
votes

It wirk! [

                'name' => 'lang_country_seller_catalog',
                'pattern' => '<lang:\w+>-<country:\w+>/seller/catalog/<module>/<controller>/<action>',
                'route' => 'seller/catalog/<module>/<controller>/<action>',
            ],
 [
                'name' => 'lang_country_seller_catalog_attributes',
                'pattern' => '<lang:\w+>-<country:\w+>/seller/catalog/attributes/<module>',
                'route' => 'seller/catalog/attributes/<module>',

            ],