- Yii 教程
- Yii - 首頁
- Yii - 概述
- Yii - 安裝
- Yii - 建立頁面
- Yii - 應用結構
- Yii - 入口指令碼
- Yii - 控制器
- Yii - 使用控制器
- Yii - 使用操作
- Yii - 模型
- Yii - 小部件
- Yii - 模組
- Yii - 檢視
- Yii - 佈局
- Yii - 資源
- Yii - 資源轉換
- Yii - 擴充套件
- Yii - 建立擴充套件
- Yii - HTTP 請求
- Yii - 響應
- Yii - URL 格式
- Yii - URL 路由
- Yii - URL 規則
- Yii - HTML 表單
- Yii - 驗證
- Yii - 即席驗證
- Yii - AJAX 驗證
- Yii - 會話
- Yii - 使用 Flash 資料
- Yii - Cookie
- Yii - 使用 Cookie
- Yii - 檔案上傳
- Yii - 格式化
- Yii - 分頁
- Yii - 排序
- Yii - 屬性
- Yii - 資料提供者
- Yii - 資料小部件
- Yii - ListView 小部件
- Yii - GridView 小部件
- Yii - 事件
- Yii - 建立事件
- Yii - 行為
- Yii - 建立行為
- Yii - 配置
- Yii - 依賴注入
- Yii - 資料庫訪問
- Yii - 資料訪問物件
- Yii - 查詢構造器
- Yii - 活動記錄
- Yii - 資料庫遷移
- Yii - 主題
- Yii - RESTful API
- Yii - RESTful API 實踐
- Yii - 欄位
- Yii - 測試
- Yii - 快取
- Yii - 片段快取
- Yii - 別名
- Yii - 日誌
- Yii - 錯誤處理
- Yii - 身份驗證
- Yii - 授權
- Yii - 本地化
- Yii - Gii
- Gii – 建立模型
- Gii – 生成控制器
- Gii – 生成模組
- Yii 有用資源
- Yii - 快速指南
- Yii - 有用資源
- Yii - 討論
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 頁面。
要暫時將您的應用程式置於維護模式,應配置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,您將看到以下內容。
建立 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() 函式的用法。
傳遞給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,您將看到以下內容。