原文連接: https://learnku.com/laravel/t...
討論請前往專業的 Laravel 開發者論壇: https://learnku.com/Laravel
Laravel 7 的正式發佈日期爲 2020年3月3日。根據 Laravel Framework 每6個月的主要 laravel 版本發佈策略(2月和 8月),它是另外一個主要版本。它不是 LTS 版本,所以根據 Laravel 版本支持政策,他們提供 2020年9月3日以前的 6個月錯誤修復,以及直到 2021年3月3日以前的 1年安全問題修復支持。讓咱們看一下 Laravel 7 的新功能和改進。php
Laravel Airlock 是用於API身份驗證的官方軟件包。它提供了簡單的令牌基礎 API 身份驗證,令牌發行,令牌能力,移動應用程序身份驗證等。laravel
使用 Zttp,向 API 發出 HTTP 請求將是更好,更簡潔的方法。數據庫
發佈請求json
<?php use Illuminate\Support\Facades\Http; $response = Http::post($url); $response = Http::post($url, [ 'site' => 'Laravel Article', ]);
獲取響應api
$response = Http::get($url); $response = Http::get($url,['foo'=>'bar']);
帶請求頭跨域
$response = Http::withHeaders(['foo' => 'bar'])->post($url, [ 'baz' => 'qux', ]);
響應安全
$response['foo'] $response->body() $response->json() $response->status() $response->ok()
如今 Laravel 7 開箱即用地支持 CORS (跨域資源共享)。 你應該更瞭解每一個開發者在 API 開發中都遇到過 CORS 問題。如今,Laravel 7 使用配置值自動響應 OPTION 請求。Laravel 7 開箱即用的 HandleCors
中間件能夠搞定一切。markdown
Laravel 7 中的自定義 eloquent casting 是另外一個很酷的功能。此功能將使您可以添加自定義 casts。讓咱們看一下 JSON Caster。less
<?php use Illuminate\Contracts\Database\Eloquent\CastsAttributes; class Json implements CastsAttributes { public function get($model, $key, $value, $attributes) { return json_decode($value, true); } public function set($model, $key, $value, $attributes) { return json_encode($value); } }
如今,咱們能夠在模型中使用自定義的 eloquent cast 了。post
<?php namespace App; use App\Casts\Json; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $casts = [ 'extra' => Json::class, ]; }
在 Laravel 7 中,您能夠使用 Illuminate\Support\Str
類來作更多更酷的面向對象的事情。
$currentVersion = (string) Str::of(' Laravel 6.x '); return $currentVersion->trim() ->replace('6.x', '7.x') ->slug(); // laravel-7x
Laravel 7 Blade X 功能使您可以製做 class-less 的組件。
生成 x-component
@php($user = $user ?? Auth::user()) @php($size = $size ?? 50) <img class="inline-block rounded-full" src="{{ $user->gravatarUrl($size) }}" width="{{ $size }}" height="{{ $size }}" />
Blade x 用法
<x-avatar/> <x-avatar size="40" /> <x-avatar size="100" />
如今,您能夠使用 artisan 命令在 Laravel 7.x 中自定義 stubs。
php artisan stub:publish
Laravel 7 提供了 withCasts
方法,可幫助您在運行查詢時強制轉換值。讓咱們舉個例子。
$users = User::select([ 'users.*', 'last_posted_at' => Post::selectRaw('MAX(created_at)')->whereColumn('user_id', 'users.id') ]) ->withCasts(['last_posted_at' => 'date']) ->get();
Laravel 7 將容許您使用單個應用程序設置多個郵件驅動程序。
Mail::mailer('noreply') ->to($request->user()) ->send(new PostUpdated($post));
Laravel 7 中添加了一個新的 artisan 測試命令。新的 artisan 測試命令爲您提供了精美的 UX 和有關測試的有用信息。
php artisan test
自定義 Key
默認狀況下,路由模型綁定與 id 字段一塊兒使用。如今您能夠自定義它。
Route::get('posts/{post:slug}', function (App\Post $post) { return $post; });
自動做用域
Laravel 7 將使用其辨別慣例使用的方法來確認程序中的關係調用,從而自動肯定查詢的範圍以檢索嵌套模型。
use App\Post; use App\User; Route::get('api/users/{user}/posts/{post:slug}', function (User $user, Post $post) { return $post; });
使用 route:cache 時,Laravel 7 的路由匹配性能比 laravel 6 快 2倍
Laravel 7 對使用 MySQL 8+ 做爲數據庫支持隊列的應用程序進行了改進。
使用 Tailwind CSS 調色板,郵件的默認 markdown 模板外觀更加新穎。能夠根據須要發佈和自定義模板。
原文連接: https://learnku.com/laravel/t...
討論請前往專業的 Laravel 開發者論壇: https://learnku.com/Laravel