Ray Lee | 李宗叡
Learn or Die
Published in
8 min readDec 9, 2020

--

Photo by Obi Onyeador on Unsplash

# 前言

我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。

# 版本

Laravel 7.x

# 目錄

Laravel — 官方文件原子化翻譯 — 目錄

# The Basics

# Generating Basic URLs

解釋以下 Laravel example

  • Example:
<?php
$post = App\Post::find(1);

echo url("/posts/{$post->id}");

// http://example.com/posts/1
  • Answer:
<?php
$post = App\Post::find(1);

// 使用 url helper 產生 url
echo url("/posts/{$post->id}");

// http://example.com/posts/1

# Accessing The Current URL

以下的 Laravel example code 的意思是?

  • Example:
<?php
echo url()->current();
  • Answer: 使用 url helper 取得當前 url, 但不包含 query string

以下的 Laravel example code 的意思是?

  • Example:
<?php
// Get the current URL including the query string...
echo url()->full();
  • Answer: 取得當前 request url, 包含 query string

以下的 Laravel example code 的意思是?

  • Example:
<?php
// Get the full URL for the previous request...
echo url()->previous();
  • Answer: 使用 url helper 取得上一次請求的 request url

Laravel 中, 除了可以使用 url helper 取得 url 之外, 我還可以使用哪一個 class?

URL facade

# URLs For Named Routes

Laravel 中, 除了使用 actual url 來產生 url 之外, 我還可以使用什麼來產生 url, 而且就算之後 actual url 變更了, 我也不會影響到我的邏輯?

可以使用 route name

以下的 example 產生 ‘example.com/post/1’ 的 url, 為什麼?

  • Example:
<?php
// 直接帶入 Post model
echo route('post.show', ['post' => $post]);
  • Answer: 因為 Laravel 會自動地從該 Post model 取出 'id'

以下的 Laravel example code 的意思是?

  • Example:
<?php
Route::get('/post/{post}/comment/{comment}', function () {
//
})->name('comment.show');
echo route('comment.show', ['post' => 1, 'comment' => 3]);
  • Answer: 產生一個 route url 'example.com/post/1/comment/3'

# Signed URLs

Laravel 中, Signed URLs 可以用在什麼情境上?

提供一個 email 到客戶信箱, “unsubscribe” 的 link

解釋以下的 Laravel example code

  • Example:
<?php
use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['user' => 1]);
  • Answer:
<?php
use Illuminate\Support\Facades\URL;

// 產生 signed url, unsubscribe 為 route name, user 為 parameter
return URL::signedRoute('unsubscribe', ['user' => 1]);

以下的 Laravel example code 的意思是?

  • Example:
<?php
use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['role' => 2]);
  • Answer: 產生一個 route name 為 unsubscribe 的 signedRoute

以下的 Laravel example code 的意思是?

  • Example:
<?php
use Illuminate\Support\Facades\URL;

return URL::temporarySignedRoute(
'unsubscribe', now()->addMinutes(30), ['role' => 2]
);
  • Answer: 產生一個 route 'unsubscribe' 的暫時 url, 有效期限為 30 分鐘, 並帶入 parameter 'role' => 2

# Validating Sighed Route Requests

以下的 Laravel example code 的意思是?

  • Example:
<?php
use Illuminate\Http\Request;

Route::get('/unsubscribe/{user}', function (Request $request) {
if (! $request->hasValidSignature()) {
abort(401);
}

// ...
})->name('unsubscribe');
  • Answer: 驗證該 User 帶進來的 signature 是否正確

解釋以下的 Laravel example code

  • Example:
<?php
protected $routeMiddleware = [
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
  • Answer: 註冊 ValidateSignature middleware 來驗證 signed url 正確性

# URLs For Controller Actions

以下的 Laravel example code 的意思是?

  • Example:
<?php
$url = action([HomeController::class], 'index');

$url = action('HomeController@index');
  • Answer: 產生一個導向 'HomeController' 的 'index' method 的 url, 兩種寫法都可

以下的 Laravel example code 的意思是?

  • Example:
<?php
$url = action('UserController@profile', ['id' => 1]);
  • Answer: 產生一個導向 'UserControlelr' 的 'profile' method 的 url, 並帶入 parameter

# Default Values

解釋以下的 Laravel example code

  • Example:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\URL;

class SetDefaultLocaleForUrls
{
public function handle($request, Closure $next)
{
URL::defaults(['locale' => $request->user()->locale]);

return $next($request);
}
}
  • Answer: 當我 return 一個 url 時, 若是該 route 需要帶入 url parameter, 我便需要每次都特別指定, 尤其若是有許多 route 都有相同的情況, 但我便需要增加很多 code 來定義回傳的 url 中 parameter 的值 可以自訂一個 middleware 統一定義 ‘locale’ 這個 url parameter, 作用範圍為回傳的 url, 與 model binding 不相衝突

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.