Yii2 动态 URL 路由规则
Yii2 Dynamic URL Routing Rules
我想在 yii2 的 Cms 页面中为 url 设置 Dynamic Routing
。
当我添加 CMS 页面时,我将添加页面别名 aboutus、faq、management 等,这些别名将保存在 db 中。
当我给 URL 静态规则时它会起作用,[检查下面的代码]
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
//'enableStrictParsing' => true,
'rules'=>array(
'aboutus'=>'cms/index/1',
'faq'=>'cms/index/2',
'termacondition'=>'cms/index/3',
'management'=>'cms/index/4',
),
],
但我想在动态中添加 url 规则。
我需要在 yii2 的 config/main.php URL 规则中添加所有动态页面别名
请帮我。
您可以在 bootstrapping 过程中编辑路由规则。
首先通过实施 yii\base\BootstrapInterface
创建 bootstrapping class
在您的组件目录下创建一个名为 DynaRoute.php
的文件
<?php
namespace app\components;
use Yii;
use yii\base\BootstrapInterface;
use app\models\Cms; // assuming Cms is the Model class for table containing aliases
class DynaRoute implements BootstrapInterface
{
public function bootstrap($app)
{
$cmsModel = Cms::find()
->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
}
}
现在在你的配置文件中(web.php 在配置文件夹中)在 $config
数组中添加上面 class 下的 bootstrap 选项
'bootstrap' => [
.... // other bootstrap options
'app\components\DynaRoute', // add this line
],
除了ck_arjun的答案之外,您还可以将参数作为ID传递给路由。
例如,如果您希望将“/aboutus”重定向到“/site/page/id/2”。
替换:
$routeArray[$row->alias] = 'YOUR_ORIGINAL_URL'
在
$routeArray[] = ['class' => 'yii\web\UrlRule', 'pattern' => $row->alias, 'route' => $row->route, 'defaults' => ['id' => $row->id]];
我想在 yii2 的 Cms 页面中为 url 设置 Dynamic Routing
。
当我添加 CMS 页面时,我将添加页面别名 aboutus、faq、management 等,这些别名将保存在 db 中。
当我给 URL 静态规则时它会起作用,[检查下面的代码]
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
//'enableStrictParsing' => true,
'rules'=>array(
'aboutus'=>'cms/index/1',
'faq'=>'cms/index/2',
'termacondition'=>'cms/index/3',
'management'=>'cms/index/4',
),
],
但我想在动态中添加 url 规则。
我需要在 yii2 的 config/main.php URL 规则中添加所有动态页面别名 请帮我。
您可以在 bootstrapping 过程中编辑路由规则。
首先通过实施 yii\base\BootstrapInterface
在您的组件目录下创建一个名为 DynaRoute.php
的文件<?php
namespace app\components;
use Yii;
use yii\base\BootstrapInterface;
use app\models\Cms; // assuming Cms is the Model class for table containing aliases
class DynaRoute implements BootstrapInterface
{
public function bootstrap($app)
{
$cmsModel = Cms::find()
->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
}
}
现在在你的配置文件中(web.php 在配置文件夹中)在 $config
数组中添加上面 class 下的 bootstrap 选项
'bootstrap' => [
.... // other bootstrap options
'app\components\DynaRoute', // add this line
],
除了ck_arjun的答案之外,您还可以将参数作为ID传递给路由。 例如,如果您希望将“/aboutus”重定向到“/site/page/id/2”。
替换:
$routeArray[$row->alias] = 'YOUR_ORIGINAL_URL'
在
$routeArray[] = ['class' => 'yii\web\UrlRule', 'pattern' => $row->alias, 'route' => $row->route, 'defaults' => ['id' => $row->id]];