首页 > 解决方案 > Yii2 路由仅带获取参数(隐藏控制器和动作)

问题描述

我看到了隐藏控制器和操作的路由,并且 url 的构造类似于www.domain.com/en/page-33/category-28/product-89?param=some_param. 在这个路由中,当我尝试获取参数时,var_dump(Yii::$app->getRequest()->getQueryParams())我得到了这样的数组:

array(4) { ["first_step"]=> string(7) "page-33" ["second_step"]=> string(11) "category-28" ['product']=> string(10) "product-89" ['param']=> string(10) "some_param"}

我怎样才能做到这一点 ?我看到了规则,它们是

'<first_step>/<second_step>/<product>/<test>/<test2>/<test3>/<test4>' => 'page/index',
                '<first_step>/<second_step>/<product>/<test>/<test2>/<test3>' => 'page/index',
                '<first_step>/<second_step>/<product>/<test>/<test2>' => 'page/index',
                '<first_step>/<second_step>/<product>/<test>' => 'page/index',
                '<first_step>/<second_step>/<product>' => 'page/index'
                '<first_step>//<product>' => 'page/index', 
                '<first_step>/<second_step>' => 'page/index',
                '<first_step>' => 'page/index'

我试图在家里这样做,但是当我转储它时Yii::$app->getRequest()->getQueryParams()它是一个空数组。这个 url 如何像 GET 参数一样制作(如果我理解正确的话)。我红色的文章是关于如何在 url 中隐藏控制器和操作,但我怎样才能做到这一点?先感谢您!PS page-33- 第一部分例如page是存储在 db 中的页面的标题,第二部分例如33是 id。

标签: routingyii2yii2-advanced-app

解决方案


我将举一个例子,我希望你能从中看到如何让它在你的具体情况下工作的模式。

假设您要实现一个简单的搜索。有您的搜索表单,您将操作的参数提交到SearchController::actionIndex(). 在这里,您可以处理发送给它的参数。

public function actionIndex()
{
    $searchForm = new SearchForm();

    if (Yii::$app->request->post()) {

        $searchForm->load(Yii::$app->request->post());
        $productType = $searchForm->productType;
        $productName = $searchForm->productName;

        $searchAttributes = $searchForm->attributes;

        unset($searchAttributes['productName']); //unset what you want to be a nicely formatted part of the url, like domain.eu/productType/productName
        unset($searchAttributes['productType']);
        foreach ($searchAttributes as $key => $value) {
            if (empty($value)) {
                unset($searchAttributes[$key]);
            }
        }

        $this->redirect(
            array_merge(
                ['/search/list', 'type' => $producType, 'name' => $productName, 
                $searchAttributes //this variable will contain all other parameters as regualer get parameters
            )
        );
}

在此之后,在 url-manager 配置文件中设置您的 url 规则,如下所示:

return [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
    [
        // /productType/productName
        'pattern' => '<type>/<name>',
        'route' => 'search/list',
        'encodeParams' => false,
        'defaults' => ['type' => null, 'name' => null],
    ],
     //add other rules as you need
]

因此,如果您的应用程序识别规则,它将解析它并将请求发送到正确的路由。

您将需要在 SearchController 中执行另一个操作:

public function actionList($type = null, $name = null) {
//do the search or anything you want to do here
    $get = Yii::$app->request->get();
    var_dump($get);
    var_dump($type); 
    var_dump($name);
 }

推荐阅读