## 注解路由
ThinkPHP支持使用注解方式定義路由(也稱為注解路由),如果需要使用注解路由需要安裝額外的擴展:
```
composer require topthink/think-annotation
```
然后只需要直接在控制器類的方法注釋中定義,例如:
~~~
<?php
namespace app\controller;
use think\annotation\Route;
class Index
{
/**
* @param string $name 數據名稱
* @return mixed
* @Route("hello/:name")
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
~~~
`@Route("hello/:name")` 就是注解路由的內容,請務必注意注釋的規范,不能在注解路由里面使用單引號,否則可能導致注解路由解析失敗,可以利用IDE生成規范的注釋。如果你使用`PHPStorm`的話,建議安裝`PHP Annotations`插件:https://plugins.jetbrains.com/plugin/7320-php-annotations ,可以支持注解的自動完成。
>[danger] 該方式定義的路由在調試模式下面實時生效,部署模式則在第一次訪問的時候生成注解緩存。
然后就使用下面的URL地址訪問:
~~~
http://tp5.com/hello/thinkphp
~~~
頁面輸出
~~~
hello,thinkphp
~~~
默認注冊的路由規則是支持所有的請求,如果需要指定請求類型,可以在第二個參數中指定請求類型:
~~~
<?php
namespace app\controller;
use think\annotation\Route;
class Index
{
/**
* @param string $name 數據名稱
* @return mixed
* @Route("hello/:name", method="GET")
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
~~~
如果有路由參數需要定義,可以直接在后面添加方法,例如:
~~~
<?php
namespace app\controller;
use think\annotation\Route;
class Index
{
/**
* @param string $name 數據名稱
* @Route("hello/:name", method="GET", https=1, ext="html")
* @return mixed
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
~~~
支持在類的注釋里面定義資源路由,例如:
~~~
<?php
namespace app\controller;
use think\annotation\route\Resource;
/**
* @Resource("blog")
*/
class Blog
{
public function index()
{
}
public function read($id)
{
}
public function edit($id)
{
}
}
~~~
如果需要定義路由分組,可以使用
~~~
<?php
namespace app\controller;
use think\annotation\route\Group;
use think\annotation\route\Route;
/**
* @Group("blog")
*/
class Blog
{
/**
* @param string $name 數據名稱
* @return mixed
* @Route("hello/:name", method="GET")
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
~~~
當前控制器中的注解路由會自動加入`blog`分組下面,最終,會注冊一個`blog/hello/:name`的路由規則。你一樣可以對該路由分組設置公共的參數,例如:
~~~
<?php
namespace app\controller;
use think\annotation\route\Middleware;
use think\annotation\route\Group;
use think\annotation\route\Route;
use think\middleware\SessionInit;
/**
* @Group("blog",ext="html")
* @Middleware({SessionInit::class})
*/
class Blog
{
/**
* @param string $name 數據名稱
* @return mixed
* @Route("hello/:name",method="GET")
*/
public function hello($name)
{
return 'hello,'.$name;
}
}
~~~
- 序言
- 基礎
- 安裝
- 開發規范
- 目錄結構
- 配置
- 架構
- 請求流程
- 架構總覽
- 入口文件
- 多應用模式
- URL訪問
- 容器和依賴注入
- 服務
- 門面
- 中間件
- 事件
- 路由
- 路由定義
- 變量規則
- 路由地址
- 路由參數
- 路由中間件
- 路由分組
- 資源路由
- 注解路由
- 路由綁定
- 域名路由
- MISS路由
- 跨域請求
- URL生成
- 控制器
- 控制器定義
- 基礎控制器
- 空控制器
- 資源控制器
- 控制器中間件
- 請求
- 請求對象
- 請求信息
- 輸入變量
- 請求類型
- HTTP頭信息
- 偽靜態
- 參數綁定
- 請求緩存
- 響應
- 響應輸出
- 響應參數
- 重定向
- 文件下載
- 數據庫
- 連接數據庫
- 分布式數據庫
- 查詢構造器
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 查詢表達式
- 鏈式操作
- where
- table
- alias
- field
- strict
- limit
- page
- order
- group
- having
- join
- union
- distinct
- lock
- cache
- cacheAlways
- comment
- fetchSql
- force
- partition
- failException
- sequence
- replace
- extra
- duplicate
- procedure
- 聚合查詢
- 分頁查詢
- 時間查詢
- 高級查詢
- 視圖查詢
- JSON字段
- 子查詢
- 原生查詢
- 獲取查詢參數
- 查詢事件
- 獲取器
- 事務操作
- 存儲過程
- 數據集
- 數據庫驅動
- 模型
- 定義
- 模型字段
- 新增
- 更新
- 刪除
- 查詢
- 查詢范圍
- JSON字段
- 獲取器
- 修改器
- 搜索器
- 數據集
- 自動時間戳
- 只讀字段
- 軟刪除
- 類型轉換
- 模型輸出
- 模型事件
- 模型關聯
- 一對一關聯
- 一對多關聯
- 遠程一對多
- 遠程一對一
- 多對多關聯
- 多態關聯
- 關聯預載入
- 關聯統計
- 關聯輸出
- 虛擬模型
- 視圖
- 模板變量
- 視圖過濾
- 模板渲染
- 模板引擎
- 視圖驅動
- 錯誤和日志
- 異常處理
- 日志處理
- 調試
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 遠程調試
- 驗證
- 驗證器
- 驗證規則
- 錯誤信息
- 驗證場景
- 路由驗證
- 內置規則
- 表單令牌
- 注解驗證
- 雜項
- 緩存
- Session
- Cookie
- 多語言
- 上傳
- 命令行
- 啟動內置服務器
- 查看版本
- 自動生成應用目錄
- 創建類庫文件
- 清除緩存文件
- 生成數據表字段緩存
- 生成路由映射緩存
- 輸出路由定義
- 自定義指令
- Debug輸出級別
- 擴展庫
- 數據庫遷移工具
- Workerman
- think助手工具庫
- 驗證碼
- Swoole
- 附錄
- 助手函數
- 升級指導
- 更新日志