# 配置路由
當我們進入一條鏈接時,應用程序做了以下處理:
1. 路由尋找 URL 對應的控制器。
2. 控制臺將指定的方法進行操作。
3. 渲染給模板。
## ThinkPHP 的路由
打開 route/route.php 看到這條語句,將路由做如下修改:
~~~
/* route/route.php */
~~Route::get('hello/:name', 'index/hello');~~
Route::get('hello', 'index/hello');
~~~
以上命令的功能:
* Route:: 注冊路由靜態方法
* get 請求方式
* 'hello' 請求 URL
* 'index/hello' 響應默認控制器中 hello 方法 (`application/index/controller/Index.php/:function hello`)
目錄: `application/index/controller/` 為默認控制器的根目錄,當我們在路由中書寫時,如果只使用默認控制器,只用寫入 控制器文件名/方法
> 并不推薦使用省略的方法使用路由語句,這是難以維護的。
你應該使用: `'index/index/hello' //模塊(index)/控制器(controller)/index.php/:function hello `
打開上述控制器文件,找到 `hello` 方法并修改為:
~~~~
/* application/index/controller/Index.php/:function hello */
~~public function hello($name = 'ThinkPHP5')~~
public function hello()
{
~~return 'hello,' . $name;~~
return 'hello world'
}
~~~~
打開瀏覽器,訪問 `http://thinkphp.test/hello` 則會出現 `hello world`,一個簡單的 GET 路由就完成了。
## 在路由中直接輸出
在閱讀本小節之前,首先要知道一個概念 閉包: http://php.net/manual/zh/functions.anonymous.php
稍作了解之后,我們來學習在路由中使用閉包。
打開路由文件,將剛才的語句進行修改:
~~~~
/* route/route.php */
~~Route::get('hello', 'index/index/hello');~~
Route::get('hello', function() {
return 'hello closures';
});
~~~~
這時候再刷新瀏覽器,輸出語句就變成了 `hello closures`
這就是路由中閉包的基本用法。
## 在控制器中響應模板
現在請刪除剛才的閉包語句,重新寫入:
~~~~
/* route/route.php */
~~Route::get('hello', function() {
return 'hello closures';
});~~
Route::get('hello', 'index/index/hello');
~~~~
進入路由對應的控制器,修改代碼:
~~~~
/* application/index/controller/Index.php/:function hello */
public function hello()
{
~~return 'hello world';~~
return view();
}
~~~~
> return view(); 是一個助手函數,表示響應對應的模板。
如果 view() 中未添加任何參數,則會響應到默認路徑:
`當前模塊/view/當前控制器名(小寫)/當前操作(小寫).html`
`application/index(當前模塊)/view/index(當前控制器名)/hello.html(當前操作)`
1. 我們創建對應路徑的對應文件 `application/index/view/index/hello.html` 并寫入 `hello view `
2. 再次刷新瀏覽器,輸出語句就變成了 `hello view `
## 在路由閉包操作中響應模板
>在實際的開發中,如果我們只是不進行任何操作的響應一個模板文件,那么使用控制器實在是有點大材小用,這時候,就需要在路由中直接輸出。
打開路由文件,將剛才的語句進行修改
~~~~
/* route/route.php */
~~Route::get('hello', 'index/index/hello');~~
Route::get('hello', function() {
return view('index@index/hello');
});
~~~~
這條語句表示該路由會渲染 `index` 模塊下面的 `view/index/hello.html` 模板文件輸出。
1. 進入到對應的模板文件,將 `hello view` 修改為 `hello router view`
2. 再次刷新瀏覽器,輸出語句就變成了 `hello router view `
## 為路由提供參數
我們在第一次對路由文件進行修改的時候,將 `Route::get('hello/:name', 'index/index/hello');` 變為了 `Route::get('hello', 'index/index/hello');`
其中,刪除了 `:name` 這幾個字符, `:name` 就是本節提到的 參數
將剛才寫入的閉包語句改為:
~~~~
/* route/route.php */
Route::get('hello/:name', function ($name) {
return 'Hello,' . $name;
});
~~~~
`function($name)` 將 `$name` 賦值為 `:name `
如果我們進入 `http://thinkphp/hello/param`,那么就會輸出 `hello param`
## 創建第一個靜態頁面
請刪除所有路由語句和 index 模塊 (`application/[index/]`)
打開路由文件,寫入語句:
~~~~
/* route/route.php */
Route::get('', 'welcome/index/home');
Route::get('/help', 'welcome/index/help');
Route::get('/about', 'welcome/index/about');
~~~~
創建控制器文件: `application/welcome/controller/Index.php`
在創建好的控制器文件中寫入:
~~~~
/* application/welcome/controller/Index.php */
namespace app\welcome\controller;
use think\Controller;
class Index extends Controller
{
public function home()
{
return '主頁';
}
public function help()
{
return '幫助';
}
public function about()
{
return '關于';
}
}
~~~~
* `namespace` 代表的是 命名空間,你可以把它理解成路徑,不同的 `namespace` 代表著不同的路徑, 只有正確路徑下的操作才能被找到。
* `use` 是使用 php 各種類的一個操作,如果我們想要使用其他封裝好的類,僅僅 `use` 對應的文件就行了。
* `class Index extends Controller` 表示當前的 Index(如果我們創建在控制器下的是 `AnyThing.php`,那么我們就要使用 `AnyThing` 而不是 `Index`) 類繼承了 `Controller` 類,如果繼承,就可以使用 `Controller` 中的所有公共方法。
現在你訪問
~~~
http://thinkphp.test
http://thinkphp.test/help
http://thinkphp.test/about
~~~
那對應就會顯示 主頁、幫助、關于。
## 渲染視圖
> 我們在前面的內容中已經學習了 在控制器中響應視圖、在路由中響應視圖, 現在只需要將 `return ''` 改為 `return view()` 即可
現在來創建模板,路徑:`application/welcome/view/index`,分別創建:
* home.html
* help.html
* about.html
~~~~
application/welcome/view/index/home.html
<!DOCTYPE html>
<html>
<head>
<title>thinkphp.test</title>
</head>
<body>
<h1>主頁</h1>
</body>
</html>
~~~~
~~~~
application/welcome/view/index/help.html
<!DOCTYPE html>
<html>
<head>
<title>thinkphp.test</title>
</head>
<body>
<h1>幫助</h1>
</body>
</html>
~~~~
~~~~
application/welcome/view/index/about.html
<!DOCTYPE html>
<html>
<head>
<title>thinkphp.test</title>
</head>
<body>
<h1>關于</h1>
</body>
</html>
~~~~
這時候再刷新瀏覽器,響應的就是視圖文件了。
## 公共視圖
>如果我們每次都在模板中寫入完整且重復的語句,那樣的工作量是巨大且低維護性的,那么現在就需要將視圖的公共部分拆分出來。
1. 現在請創建一個公共模板的文件夾:`application/welcome/view/_layout`
2. 并在該目錄下創建一個 `default.html` 的文件并寫入
~~~~
<!DOCTYPE html>
<html>
<head>
<title>thinkphp.test</title>
</head>
<body>
<h1>{block name="content"}正文{/block}</h1>
</body>
</html>
~~~~
然后點開 `home.html`,將內容修改為:
~~~~
{extend name="_layout/default" /}
{block name="content"}
主頁
{/block}
~~~~
其他兩個頁面也像這樣修改,只是 content 里的文字變化一下。
修改完成后,刷新瀏覽器,現在模板成功的繼承并且輸出了!
- 第一章. 基礎信息
- 1.1 序言
- 1.2 關于作者
- 1.3 本書源碼
- 1.4 反饋糾錯
- 1.5 安全指南
- 1.6 捐助作者
- 第二章. 開發環境布置
- 2.1 編輯器選用
- 2.2 命令行工具
- 2.3 開發環境搭建
- 2.4 瀏覽器選擇
- 2.5 第一個應用
- 2.6 Git 工作流
- 第三章. 構建頁面
- 3.1 章節說明
- 3.2 靜態頁面
- 3.3 Think 命令
- 3.4 小結
- 第四章. 優化頁面
- 4.1 章節說明
- 4.2 樣式美化
- 4.3 局部視圖
- 4.4 路由鏈接
- 4.5 用戶注冊頁面
- 4.6 集中視圖
- 4.7 小結
- 第五章. 用戶模型
- 5.1 章節說明
- 5.2 數據庫遷移
- 5.3 查看數據表
- 5.4 模型文件
- 5.5 小結
- 第六章. 用戶注冊
- 6.1 章節說明
- 6.2 注冊表單
- 6.3 用戶數據驗證
- 6.4 注冊失敗錯誤信息
- 6.5 注冊成功
- 6.6 小結
- 第七章. 會話管理
- 7.1 章節說明
- 7.2 會話
- 7.3 用戶登錄
- 7.4 退出
- 7.5 小結
- 第八章. 用戶 CRUD
- 8.1 章節說明
- 8.2 重構代碼
- 8.3 更新用戶
- 8.4 權限系統
- 8.5 列出所有用戶
- 8.6 刪除用戶
- 8.7 訪客模式
- 8.8 優化前端
- 8.9 小結
- 第九章. 微博 CRUD
- 9.1 章節說明
- 9.2 微博模型
- 9.3 顯示微博
- 9.4 發布微博
- 9.5 微博數據流
- 9.6 刪除微博
- 9.7 小結