Yii2 Dynamic URL Routing Rules

后端 未结 2 1433
广开言路
广开言路 2020-12-17 05:22

I want to set Dynamic Routing for url in Cms pages in yii2. When i add Cms page i will add page alias aboutus,faq,management etc , these alias will saved in db

2条回答
  •  不知归路
    2020-12-17 06:08

    You can edit you routing rules during bootstrapping process.

    First create bootstrapping class by implementing yii\base\BootstrapInterface

    Under Your components directory create a file called DynaRoute.php

    all(); // customize the query according to your need
            routeArray = [];
            foeach($cmsModel as $row) { // looping through each cms table row
                $routeArray[$row->alias] = 'YOUR_ORIGINAL_URL'; // Adding rules to array on by one
            }
            $app->urlManager->addRules($routeArray);// Append new rules to original rules
    }     
    

    }

    Now in your configuration file (web.php in config folder) in $config array add above class under bootstrap option

    'bootstrap' => [
        .... // other bootstrap options 
        'app\components\DynaRoute', // add this line 
    ],
    

提交回复
热议问题