無涯教程: Laravel 8 - 新功能介紹


新版本在模型中添加了更多的增強函數,.env,rate limiting,route cacheing,factory,controller namespace命名空間等。

*)默認Model模型目錄

Laravel 8版本將為所有Eloquent模型創建新目錄"Models"。

目前,所有模型都是默認存儲在app/user.php,app/post.php等文件夾中。但是Laravel 8提供新目錄"Models"以將所有模型存儲在該文件夾。

舊Model路徑:

app/User.php

app/Post.php

新Model路徑:

app/Models/User.php

app/Models/Post.php

*)增強PHP Artisan服務

php artisan serve

在Laravel8之前,你在.env配置了變量值,需要重新服務命令,在Laravel8中,變量值將自動更新。

*)刪除控制器命名空間前綴

Laravel 8從以前版本的Laravel中刪除了來自RouteserviceProvider.php文件的$名稱空間變量前綴。因此,基本上它將自動將"App\Htpp\Controllers"命名空間應用於控制器。

舊文件 RouteServiceProvider.php:

 <?php

 

namespace App\Providers;

 

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use Illuminate\Support\Facades\Route;

 

class RouteServiceProvider extends ServiceProvider

{

/**

* This namespace is applied to your controller routes.

*

* In addition, it is set as the URL generator's root namespace.

*

* @var string

*/

protected $namespace = 'App\Http\Controllers';

 

/**

* The path to the "home" route for your application.

*

* @var string

*/

public const HOME = '/home';

....

新文件 RouteServiceProvider.php:

 <?php

 

namespace App\Providers;

 

use Illuminate\Cache\RateLimiting\Limit;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\RateLimiter;

use Illuminate\Support\Facades\Route;

 

class RouteServiceProvider extends ServiceProvider

{

/**

* The path to the "home" route for your application.

*

* This is used by Laravel authentication to redirect users after login.

*

* @var string

*/

public const HOME = '/home';

 

/**

* Define your route model bindings, pattern filters, etc.

*

* @return void

*/

public function boot()

{

$this->configureRateLimiting();

 

$this->routes(function () {

Route::middleware('web')

->group(base_path('routes/web.php'));

 

Route::prefix('api')

->middleware('api')

->group(base_path('routes/api.php'));

});

}

 

....

*)增強速率限制

速率限制可以在路由中。也可以應用到中間件中,以便設置每次的請求數。

RouteServiceProvider.php

<?php

namespace App\Providers;

 

use Illuminate\Cache\RateLimiting\Limit;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\RateLimiter;

use Illuminate\Support\Facades\Route;

 

class RouteServiceProvider extends ServiceProvider

{

/**

* The path to the "home" route for your application.

*

* This is used by Laravel authentication to redirect users after login.

*

* @var string

*/

public const HOME = '/home';

 

/**

* Define your route model bindings, pattern filters, etc.

*

* @return void

*/

public function boot()

{

RateLimiter::for('uploadFile' function (Request $request) {

Limit::perMinute(8)->by($request->ip()),

});

 

RateLimiter::for('downloadFile' function (Request $request) {

if ($request->user()->isSubscribed()) {

return Limit::none();

}

Limit::perMinute(8)->by($request->ip()),

});

 

$this->configureRateLimiting();

 

$this->routes(function () {

Route::middleware('web')

->group(base_path('routes/web.php'));

 

Route::prefix('api')

->middleware('api')

->group(base_path('routes/api.php'));

});

}

 

/**

* Configure the rate limiters for the application.

*

* @return void

*/

protected function configureRateLimiting()

{

RateLimiter::for('api' function (Request $request) {

return Limit::perMinute(60);

});

}

}

使用速率限制(Rate Limit)

Route::get('upload','[email protected]')->->middleware('throttle:uploadFile');

Route::get('download','[email protected]')->->middleware('throttle:downloadFile');

 

/* or use it no group */

Route::middleware(['throttle:uploadFile'])->group(function () {

 

});

*)Route Caching 增強

我們使用"php artisan route:cache"命令緩存路由。在laravel的早期版本中,緩存路由后,如果您要添加新路由,那么將需要執行手動清除再生成命令。但在Laravel8中自動將新路由添加到緩存文件中。

*)更新分頁設計

laravel 8默認的分頁類已刪除,使用tailwind框架作為默認分類.。如果要使用Bootstrap框架,則需要在AppServiceProvider文件中調用"useBootstrap()"。

<?php

 

namespace App\Providers;

 

use Illuminate\Support\ServiceProvider;

use Illuminate\Pagination\Paginator;

 

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

 

}

 

/**

* Bootstrap any application services.

*

* @return void

*/

public function boot()

{

Paginator::useBootstrap();

}

}

*)更新關閉事件偵聽器

Laravel 8更改了語法來調用事件偵聽器。所以我可以向你展示具有舊語法的示例:

舊語法:

Event::listen(OrderShipped::class, function(OrderShipped $event) {

//Do something

});

新語法:

Event::listen(function(OrderShipped $event) {

/* Do something */

});

*)隊列事件監聽器

當調用模型創建(creating),創建(created),更新(updating),更新(updated)等事件時,可以輕松地將其排入隊列。

讓我們來看一個例子:

Product Model:

class Product extends Model {

 

protected static function booting()

{

static::created(queueable(function(Product $product) {

/* Write something Here */

}))

 

static::updated(queueable(function(Product $product) {

/* Write something Here */

}))

}

 

}

*)維護模式:密碼訪問

如果您想要網站關閉和開啟,那么我們正在使用以下命令:

php artisan down

php artisan up

Laravel 8提供了一種方法,可以為很多人授予訪問權限的密碼。您可以使用密碼並忽略cookie,因此直到您的網站啟動,他們才可以訪問舊版本。

php artisan down --secret=new-pass

現在它將創建新的路由,如果您訪問此路由,則它將忽略cookie並通過以下URL訪問網站:

https://www.example.com/new-pass

*)維護模式:預呈現頁面

Laravel 8添加了新選項,可以在您的網站關閉時立即返回頁面。您可以將render選項與文件路徑一起使用,它將顯示該文件,直到網站上線(up):

php artisan down --render="errors::backSoon"

您也可以使用命令:

php artisan down --redirect=/ --status=200 --secret=myPassword --render="errors::503"

*)隊列批處理

Laravel 8提供了新函數“排隊作業批處理",因此您可以一次將多個作業批量添加到隊列中。有一個then(),catch()和finally()回調將在所有作業完成時觸發。

Bus::batch([

new SendMailJob(),

new SendMailJob(),

new SendMailJob(),

new SendMailJob(),

new SendMailJob(),

])->then(function(Batch $batch){

//隊列執行成功時調用

})->catch(function(Batch $batch) {

//隊列執行失敗時調用

})->finally(function(Batch $batch) {

//不管失敗還是成功都會調用

})->dispatch();

*)隊列backoff()

當您可以在Queue作業類中定義時,Laravel 8添加了新的方法backoff()。您可以使用數組定義編號時間以設置重試時間。

您可以像下面這樣稱呼工作:

class ExampleJob

{

/**

* Calculate the number of seconds to wait before retrying the job.

*

* @return array

*/

public function backoff()

{

return [1, 5, 10];

}

 

....

}

*)Laravel Factory

Laravel 8模型對工廠進行了改進,因此您可以輕松地從工廠創建新的虛擬記錄。他們添加了新的times(),以便您可以定義創建的記錄數選項。

Route::get('test-factory',function(){

return User::factory()->create();

});

laravel 8添加了更多函數,您還可以從此處查看laravel 8升級指南:Laravel升級指南


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM