0

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

4
  • Do you have other rules ? Show us your urlManager config. Commented Jan 21, 2016 at 16:28
  • Have you tried changing the regular expression from [\w-]+ to something else. For eg. \w+ or just use <alias> without any expression. Commented Jan 22, 2016 at 6:19
  • @soju see my updated question Commented Jan 22, 2016 at 6:22
  • @Hanafi yes i have tried <alias:\w+>, <alias:.*>, <alias> Commented Jan 22, 2016 at 6:24

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

            ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.