【问题标题】:Yii2 url rule for module and parametersYii2 模块和参数的 url 规则
【发布时间】:2016-01-21 15:33:58
【问题描述】:

我正在尝试以一种方式配置 Yii2 url 管理器,如果在 url 中跳过控制器名称,它应该调用默认控制器进行操作。我已经设法在没有操作参数的情况下实现了这一点。但是在动作名称中使用参数时卡住了。

这是我的路线配置:

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

控制器文件:

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);
    }
} 

当我访问以下网址时,它会完美加载。 http://domain.com/index.php/catalog/shopbydepartment

但是当我访问下面的 url 时,它调用了正确的函数,但没有传递 $alias 值: http://domain.com/index.php/catalog/category/appliances

更新:

我使用以下方法进行模块明智的 url 规则声明: https://stackoverflow.com/a/27959286/1232366

这是我在主配置文件中的内容:

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

管理员工作正常,这是我的第一个模块,所以已经提到了其余规则

【问题讨论】:

  • 你还有其他规定吗?向我们展示您的 urlManager 配置。
  • 您是否尝试将正则表达式从 [\w-]+ 更改为其他内容。例如。 \w+ 或者只使用 而不使用任何表达式。
  • @soju 查看我更新的问题
  • @Hanafi 是的,我尝试过 , <.>

标签: php url-rewriting yii2


【解决方案1】:

只是为了帮助其他人,我使用以下方法检索了 $alias 的值:

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

但这绝对不是问题的准确答案。我仍然不知道我做错了什么,我没有使用所提到的方法获得价值。

【讨论】:

    【解决方案2】:

    它好用! [

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 2017-04-17
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多