[TOC]
# URL
## 基礎
```
// 生成基礎 URL
$post = App\Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1
// 訪問當前 URL
echo url()->current(); //不包含查詢參數
echo url()->full(); //包含查詢參數
echo url()->previous(); //上一個請求連接,包含查詢參數
// 通過 URL facade 訪問
use Illuminate\Support\Facades\URL;
echo URL::current();
```
## 命名路由的 URL
輔助函數`route`使用命名路由生成 URL,不與路由上定義的 URL 相耦合。
```
Route::get('/post/{post}', function () {
// 單個參數
})->name('post.show');
echo route('post.show', ['post' => 1]);
// http://example.com/post/1
Route::get('/post/{post}/comment/{comment}', function () {
// 多個參數
})->name('comment.show');
echo route('comment.show', ['post' => 1, 'comment' => 3]);
// http://example.com/post/1/comment/3
// 使用 Eloquent 模型 的主鍵生成 URL,自動提取模型的主鍵
echo route('post.show', ['post' => $post]);
```
### 簽名 URL
```
// http://test.com/unsubscribe/1?signature=簽名
use Illuminate\Support\Facades\URL;
return URL::signedRoute('unsubscribe', ['user' => 1]);
// 設置有效期
// http://test.com/unsubscribe/1?expires=1608476953&signature=簽名
return URL::signedRoute('unsubscribe', ['user' => 1], now()->addMinutes(30));
// 使用 temporarySignedRoute 方法
return URL::temporarySignedRoute(
'unsubscribe', now()->addMinutes(30), ['user' => 1]
);
```
### 驗證簽名路由請求
一、Request 驗證
```
use Illuminate\Http\Request;
Route::get('/unsubscribe/{user}', function (Request $request) {
if (! $request->hasValidSignature()) {
abort(401);
}
// ...
})->name('unsubscribe');
```
二、中間件驗證
```
// 在 app/Http/Kernel.php 中的 $routeMiddleware 屬性中增加中間件,默認已存在
protected $routeMiddleware = [
// ...
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
// 驗證失敗返回403錯誤響應
Route::post('/unsubscribe/{user}', function (Request $request) {
// ...
})->name('unsubscribe')->middleware('signed');
```
## 控制器行為的 URL
```
$url = action('HomeController@index');
use App\Http\Controllers\HomeController;
$url = action([HomeController::class, 'index']);
// 傳遞參數
$url = action('UserController@profile', ['id' => 1]);
```
## 默認值
[參考文章](https://learnku.com/articles/43638)
```
// 定義路由
Route::get('/{locale}/posts', 'IndexController@posts')->name('posts');
// 定義中間件
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetDefaultLocaleForUrls
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
URL::defaults(['locale' => $request->user()->locale ?? 'cn']);
return $next($request);
}
}
// 注冊中間件,在 app/Http/Kernel.php 中的 $routeMiddleware 屬性中增加中間件
protected $routeMiddleware = [
// ...
'locale' => \App\Http\Middleware\SetDefaultLocaleForUrls::class,
];
// 在路由或控制器中配置中間件
Route::get('/', 'IndexController@index')->middleware('locale');
public function __construct()
{
$this->middleware('locale')->only('index');
}
// 在控制器中使用,不需要設置 locale 參數
// http://test.com/cn/posts
public function index()
{
return route('posts');
}
```
- 入門指南
- 安裝
- 部署
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- URL
- Session
- 表單驗證
- 錯誤
- 日志
- 前端開發
- Blade 模板
- 本地化
- 腳手架
- 編譯資源 Mix
- 安全相關
- 用戶認證
- API 認證
- 綜合話題
- 命令行
- 廣播
- 緩存
- 集合
- 事件
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 速查表
- Artisan
- Auth
- Blade
- Cache
- Collection
- Composer
- Config
- Container
- Cookie
- DB
- Environment
- Event
- File
- Helper
- Input
- Lang
- Log
- Model
- Pagination
- Queue
- Redirect
- Request
- Response
- Route
- SSH
- Schema
- Security
- Session
- Storage
- String
- URL
- UnitTest
- Validation
- View