📜  Yii-URL路由

📅  最后修改于: 2020-10-16 07:12:45             🧑  作者: Mango


要更改应用程序的默认路由,应配置defaultRoute属性。

步骤1-通过以下方式修改config / web.php文件。

 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'defaultRoute' => 'site/contact',
      'components' => [
         //other code
?>

步骤2-转到http:// localhost:8080 / index.php 。您将看到默认的联系页面。

联系页面

要暂时将应用程序置于维护模式,您应该配置yii \ web \ Application :: $ catchAll属性。

步骤3-将以下函数添加到SiteController

public function actionMaintenance() {
   echo "

Maintenance

"; }

步骤4-然后,以以下方式修改config / web.php文件。

 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'catchAll' => ['site/maintenance'],
      'components' => [
         //OTHER CODE

步骤5-现在输入应用程序的任何URL,您将看到以下内容。

保养

创建URL

要创建各种URL,可以使用yii \ helpers \ Url :: to()帮助器方法。下面的示例假定使用默认的URL格式。

步骤1-actionRoutes()方法添加到SiteController

public function actionRoutes() {
   return $this->render('routes');
}

此方法仅呈现路线视图。

步骤2-在views / site目录中,使用以下代码创建一个名为routes.php的文件。



Url::to(['post/index']):

Url::to(['post/view', 'id' => 100]): 100]); ?>

Url::to(['post/view', 'id' => 100, '#' => 'content']): 100, '#' => 'content']); ?>

Url::to(['post/index'], true):

Url::to(['post/index'], 'https'):

步骤3-键入http:// localhost:8080 / index.php?r = site / routes ,您将看到to()函数的一些用法。

发挥作用

根据以下规则,传递给yii \ helpers \ Url :: to()方法的路由可以是相对的或绝对的-

  • 如果路由为空,将使用当前请求的路由。

  • 如果路由没有前导斜线,则将其视为相对于当前模块的路由。

  • 如果路由中不包含斜杠,则将其视为当前控制器的操作ID。

yii \ helpers \ Url helper类还提供了几种有用的方法。

步骤4-修改路由视图,如以下代码所示。



Url::home():

Url::base():

Url::canonical():

Url::previous():

步骤5-如果在网络浏览器中输入地址http:// localhost:8080 / index.php?r = site / routes ,您将看到以下内容。

修改后的路线视图输出