Yii - URL 路由



要更改應用程式的預設路由,應配置defaultRoute 屬性。

步驟 1 - 以如下方式修改config/web.php 檔案。

<?php
   $params = require(__DIR__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'defaultRoute' => 'site/contact',
      'components' => [
         //other code
?>

步驟 2 - 轉到https://:8080/index.php。您將看到預設的contact 頁面。

Contact Page

要暫時將您的應用程式置於維護模式,應配置yii\web\Application::$catchAll 屬性。

步驟 3 - 將以下函式新增到SiteController

public function actionMaintenance() {
   echo "<h1>Maintenance</h1>";
}

步驟 4 - 然後,以如下方式修改config/web.php 檔案。

<?php
   $params = require(__DIR__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'catchAll' => ['site/maintenance'],
      'components' => [
         //OTHER CODE

步驟 5 - 現在輸入應用程式的任何 URL,您將看到以下內容。

Maintenance

建立 URL

要建立各種型別的 URL,可以使用yii\helpers\Url::to() 輔助方法。以下示例假設使用預設 URL 格式。

步驟 1 - 向SiteController 新增一個actionRoutes() 方法。

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

此方法只需呈現routes 檢視。

步驟 2 - 在 views/site 目錄內,建立一個名為routes.php 的檔案,其中包含以下程式碼。

<?php
   use yii\helpers\Url;
?>

<h4>
   <b>Url::to(['post/index']):</b>
   <?php
      // creates a URL to a route: /index.php?r = post/index
      echo Url::to(['post/index']);
   ?>
</h4>

<h4>
   <b>Url::to(['post/view', 'id' => 100]):</b>
   <?php
      // creates a URL to a route with parameters: /index.php?r = post/view&id=100
      echo Url::to(['post/view', 'id' => 100]);
   ?>
</h4>

<h4>
   <b>Url::to(['post/view', 'id' => 100, '#' => 'content']):</b>
   <?php
      // creates an anchored URL: /index.php?r = post/view&id=100#content
      echo Url::to(['post/view', 'id' => 100, '#' => 'content']);
   ?>
</h4>

<h4>
   <b>Url::to(['post/index'], true):</b>
   <?php
      // creates an absolute URL: http://www.example.com/index.php?r=post/index
      echo Url::to(['post/index'], true);
   ?>
</h4>

<h4>
   <b>Url::to(['post/index'], 'https'):</b>
   <?php
      // creates an absolute URL using the https scheme: https://www.example.com/index.php?r=post/index
      echo Url::to(['post/index'], 'https');
   ?>
</h4>

步驟 3 - 輸入https://:8080/index.php?r=site/routes,您將看到一些to() 函式的用法。

to Function

傳遞給yii\helpers\Url::to() 方法的路由可以根據以下規則是相對的或絕對的:

  • 如果路由為空,則使用當前請求的路由。

  • 如果路由沒有前導斜槓,則將其視為相對於當前模組的路由。

  • 如果路由不包含斜槓,則將其視為當前控制器的操作 ID。

yii\helpers\Url 輔助類還提供了一些有用的方法。

步驟 4 - 按以下程式碼修改routes 檢視。

<?php
   use yii\helpers\Url;
?>

<h4>
   <b>Url::home():</b>
   <?php
      // home page URL: /index.php?r=site/index
      echo Url::home();
   ?>
</h4>
 
<h4>
   <b>Url::base():</b>
   <?php
      // the base URL, useful if the application is deployed in a sub-folder of the Web root
      echo Url::base();
   ?>
</h4>
 
<h4>
   <b>Url::canonical():</b>
   <?php
      // the canonical URL of the currently requested URL
      // see https://en.wikipedia.org/wiki/Canonical_link_element
      echo Url::canonical();
   ?>
</h4>
 
<h4>
   <b>Url::previous():</b>
   <?php
      // remember the currently requested URL and retrieve it back in later requests
      Url::remember();
      echo Url::previous();
   ?>
</h4>

步驟 5 - 如果您在 Web 瀏覽器中輸入地址https://:8080/index.php?r=site/routes,您將看到以下內容。

Modified Routes View Outputs
廣告
© . All rights reserved.