版本資訊
版本策略
Laravel 及其第一方套件都遵守 語義化版本。框架的主要更新會每年釋出 (約在第一季),而次版本與修訂版則可能頻繁到每週更新。此版本與修訂版 絕對不會 包含中斷性變更 (Breaking Change)。
由於 Laravel 的主要更新會包含中斷性變更,因此在專案或套件中參照 Laravel 框架或其組件時,應使用如 ^11.0
這樣的版本限制式。不過,我們也會不斷努力確保每次進行主要版本更新時,都可於一天之內升級完成。
帶名稱的引數
帶名稱引數功能尚未包含在 Laravel 的向下相容性方針內。我們可能會在有必要的時候更改函式的參數名稱以改進 Laravel 的程式碼。因此,在使用帶名稱引數呼叫 Laravel 方法時應格外注意,並瞭解到引數名稱未來可能會有所更改。
支援政策
所有的 Laravel 版本都提供 18 個月的 Bug 修正,以及 2 年的安全性修正。對於其他的函式庫,如 Lumen,則只有最新的主要版本會收到 Bug 修正。此外,也請參考 Laravel 支援的資料庫版本。
Laravel 11
Laravel 11 continues the improvements made in Laravel 10.x by introducing a streamlined application structure, per-second rate limiting, health routing, graceful encryption key rotation, queue testing improvements, Resend mail transport, Prompt validator integration, new Artisan commands, and more. In addition, Laravel Reverb, a first-party, scalable WebSocket server has been introduced to provide robust real-time capabilities to your applications.
PHP 8.2
Laravel 11.x 所要求的最小 PHP 版本為 8.2。
Streamlined Application Structure
Laravel's streamlined application structure was developed by Taylor Otwell and Nuno Maduro.
Laravel 11 introduces a streamlined application structure for new Laravel applications, without requiring any changes to existing applications. The new application structure is intended to provide a leaner, more modern experience, while retaining many of the concepts that Laravel developers are already familiar with. Below we will discuss the highlights of Laravel's new application structure.
The Application Bootstrap File
The bootstrap/app.php
file has been revitalized as a code-first application configuration file. From this file, you may now customize your application's routing, middleware, service providers, exception handling, and more. This file unifies a variety of high-level application behavior settings that were previously scattered throughout your application's file structure:
1return Application::configure(basePath: dirname(__DIR__))2 ->withRouting(3 web: __DIR__.'/../routes/web.php',4 commands: __DIR__.'/../routes/console.php',5 health: '/up',6 )7 ->withMiddleware(function (Middleware $middleware) {8 //9 })10 ->withExceptions(function (Exceptions $exceptions) {11 //12 })->create();
1return Application::configure(basePath: dirname(__DIR__))2 ->withRouting(3 web: __DIR__.'/../routes/web.php',4 commands: __DIR__.'/../routes/console.php',5 health: '/up',6 )7 ->withMiddleware(function (Middleware $middleware) {8 //9 })10 ->withExceptions(function (Exceptions $exceptions) {11 //12 })->create();
Service Providers
Instead of the default Laravel application structure containing five service providers, Laravel 11 only includes a single AppServiceProvider
. The functionality of the previous service providers has been incorporated into the bootstrap/app.php
, is handled automatically by the framework, or may be placed in your application's AppServiceProvider
.
For example, event discovery is now enabled by default, largely eliminating the need for manual registration of events and their listeners. However, if you do need to manually register events, you may simply do so in the AppServiceProvider
. Similarly, route model bindings or authorization gates you may have previously registered in the AuthServiceProvider
may also be registered in the AppServiceProvider
.
Opt-in API and Broadcast Routing
The api.php
and channels.php
route files are no longer present by default, as many applications do not require these files. Instead, they may be created using simple Artisan commands:
1php artisan install:api23php artisan install:broadcasting
1php artisan install:api23php artisan install:broadcasting
Middleware
Previously, new Laravel applications included nine middleware. These middleware performed a variety of tasks such as authenticating requests, trimming input strings, and validating CSRF tokens.
In Laravel 11, these middleware have been moved into the framework itself, so that they do not add bulk to your application's structure. New methods for customizing the behavior of these middleware have been added to the framework and may be invoked from your application's bootstrap/app.php
file:
1->withMiddleware(function (Middleware $middleware) {2 $middleware->validateCsrfTokens(3 except: ['stripe/*']4 );56 $middleware->web(append: [7 EnsureUserIsSubscribed::class,8 ])9})
1->withMiddleware(function (Middleware $middleware) {2 $middleware->validateCsrfTokens(3 except: ['stripe/*']4 );56 $middleware->web(append: [7 EnsureUserIsSubscribed::class,8 ])9})
Since all middleware can be easily customized via your application's bootstrap/app.php
, the need for a separate HTTP "kernel" class has been eliminated.
Scheduling
Using a new Schedule
facade, scheduled tasks may now be defined directly in your application's routes/console.php
file, eliminating the need for a separate console "kernel" class:
1use Illuminate\Support\Facades\Schedule;23Schedule::command('emails:send')->daily();
1use Illuminate\Support\Facades\Schedule;23Schedule::command('emails:send')->daily();
Exception Handling
Like routing and middleware, exception handling can now be customized from your application's bootstrap/app.php
file instead of a separate exception handler class, reducing the overall number of files included in a new Laravel application:
1->withExceptions(function (Exceptions $exceptions) {2 $exceptions->dontReport(MissedFlightException::class);34 $exceptions->report(function (InvalidOrderException $e) {5 // ...6 });7})
1->withExceptions(function (Exceptions $exceptions) {2 $exceptions->dontReport(MissedFlightException::class);34 $exceptions->report(function (InvalidOrderException $e) {5 // ...6 });7})
Base Controller
Class
The base controller included in new Laravel applications has been simplified. It no longer extends Laravel's internal Controller
class, and the AuthorizesRequests
and ValidatesRequests
traits have been removed, as they may be included in your application's individual controllers if desired:
1<?php23namespace App\Http\Controllers;45abstract class Controller6{7 //8}
1<?php23namespace App\Http\Controllers;45abstract class Controller6{7 //8}
Application Defaults
By default, new Laravel applications use SQLite for database storage, as well as the database
driver for Laravel's session, cache, and queue. This allows you to begin building your application immediately after creating a new Laravel application, without being required to install additional software or create additional database migrations.
In addition, over time, the database
drivers for these Laravel services have become robust enough for production usage in many application contexts; therefore, they provide a sensible, unified choice for both local and production applications.
Laravel Reverb
Laravel Reverb was developed by Joe Dixon.
Laravel Reverb brings blazing-fast and scalable real-time WebSocket communication directly to your Laravel application, and provides seamless integration with Laravel’s existing suite of event broadcasting tools, such as Laravel Echo.
1php artisan reverb:start
1php artisan reverb:start
In addition, Reverb supports horizontal scaling via Redis's publish / subscribe capabilities, allowing you to distribute your WebSocket traffic across multiple backend Reverb servers all supporting a single, high-demand application.
For more information on Laravel Reverb, please consult the complete Reverb documentation.
Per-Second Rate Limiting
Per-second rate limiting was contributed by Tim MacDonald.
Laravel now supports "per-second" rate limiting for all rate limiters, including those for HTTP requests and queued jobs. Previously, Laravel's rate limiters were limited to "per-minute" granularity:
1RateLimiter::for('invoices', function (Request $request) {2 return Limit::perSecond(1);3});
1RateLimiter::for('invoices', function (Request $request) {2 return Limit::perSecond(1);3});
For more information on rate limiting in Laravel, check out the rate limiting documentation.
Health Routing
Health routing was contributed by Taylor Otwell.
New Laravel 11 applications include a health
routing directive, which instructs Laravel to define a simple health-check endpoint that may be invoked by third-party application health monitoring services or orchestration systems like Kubernetes. By default, this route is served at /up
:
1->withRouting(2 web: __DIR__.'/../routes/web.php',3 commands: __DIR__.'/../routes/console.php',4 health: '/up',5)
1->withRouting(2 web: __DIR__.'/../routes/web.php',3 commands: __DIR__.'/../routes/console.php',4 health: '/up',5)
When HTTP requests are made to this route, Laravel will also dispatch a DiagnosingHealth
event, allowing you to perform additional health checks that are relevant to your application.
Graceful Encryption Key Rotation
Graceful encryption key rotation was contributed by Taylor Otwell.
Since Laravel encrypts all cookies, including your application's session cookie, essentially every request to a Laravel application relies on encryption. However, because of this, rotating your application's encryption key would log all users out of your application. In addition, decrypting data that was encrypted by the previous encryption key becomes impossible.
Laravel 11 allows you to define your application's previous encryption keys as a comma-delimited list via the APP_PREVIOUS_KEYS
environment variable.
When encrypting values, Laravel will always use the "current" encryption key, which is within the APP_KEY
environment variable. When decrypting values, Laravel will first try the current key. If decryption fails using the current key, Laravel will try all previous keys until one of the keys is able to decrypt the value.
This approach to graceful decryption allows users to keep using your application uninterrupted even if your encryption key is rotated.
For more information on encryption in Laravel, check out the encryption documentation.
Automatic Password Rehashing
Automatic password rehashing was contributed by Stephen Rees-Carter.
Laravel's default password hashing algorithm is bcrypt. The "work factor" for bcrypt hashes can be adjusted via the config/hashing.php
configuration file or the BCRYPT_ROUNDS
environment variable.
Typically, the bcrypt work factor should be increased over time as CPU / GPU processing power increases. If you increase the bcrypt work factor for your application, Laravel will now gracefully and automatically rehash user passwords as users authenticate with your application.
Prompt Validation
Prompt validator integration was contributed by Andrea Marco Sartori.
Laravel Prompts is a PHP package for adding beautiful and user-friendly forms to your command-line applications, with browser-like features including placeholder text and validation.
Laravel Prompts supports input validation via closures:
1$name = text(2 label: 'What is your name?',3 validate: fn (string $value) => match (true) {4 strlen($value) < 3 => 'The name must be at least 3 characters.',5 strlen($value) > 255 => 'The name must not exceed 255 characters.',6 default => null7 }8);
1$name = text(2 label: 'What is your name?',3 validate: fn (string $value) => match (true) {4 strlen($value) < 3 => 'The name must be at least 3 characters.',5 strlen($value) > 255 => 'The name must not exceed 255 characters.',6 default => null7 }8);
However, this can become cumbersome when dealing with many inputs or complicated validation scenarios. Therefore, in Laravel 11, you may utilize the full power of Laravel's validator when validating prompt inputs:
1$name = text('What is your name?', validate: [2 'name' => 'required|min:3|max:255',3]);
1$name = text('What is your name?', validate: [2 'name' => 'required|min:3|max:255',3]);
Queue Interaction Testing
Queue interaction testing was contributed by Taylor Otwell.
Previously, attempting to test that a queued job was released, deleted, or manually failed was cumbersome and required the definition of custom queue fakes and stubs. However, in Laravel 11, you may easily test for these queue interactions using the withFakeQueueInteractions
method:
1use App\Jobs\ProcessPodcast;23$job = (new ProcessPodcast)->withFakeQueueInteractions();45$job->handle();67$job->assertReleased(delay: 30);
1use App\Jobs\ProcessPodcast;23$job = (new ProcessPodcast)->withFakeQueueInteractions();45$job->handle();67$job->assertReleased(delay: 30);
For more information on testing queued jobs, check out the queue documentation.
New Artisan Commands
Class creation Artisan commands were contributed by Taylor Otwell.
New Artisan commands have been added to allow the quick creation of classes, enums, interfaces, and traits:
1php artisan make:class2php artisan make:enum3php artisan make:interface4php artisan make:trait
1php artisan make:class2php artisan make:enum3php artisan make:interface4php artisan make:trait
Model Casts Improvements
Model casts improvements were contributed by Nuno Maduro.
Laravel 11 supports defining your model's casts using a method instead of a property. This allows for streamlined, fluent cast definitions, especially when using casts with arguments:
1/**2 * Get the attributes that should be cast.3 *4 * @return array<string, string>5 */6protected function casts(): array7{8 return [9 'options' => AsCollection::using(OptionCollection::class),10 // AsEncryptedCollection::using(OptionCollection::class),11 // AsEnumArrayObject::using(OptionEnum::class),12 // AsEnumCollection::using(OptionEnum::class),13 ];14}
1/**2 * Get the attributes that should be cast.3 *4 * @return array<string, string>5 */6protected function casts(): array7{8 return [9 'options' => AsCollection::using(OptionCollection::class),10 // AsEncryptedCollection::using(OptionCollection::class),11 // AsEnumArrayObject::using(OptionEnum::class),12 // AsEnumCollection::using(OptionEnum::class),13 ];14}
For more information on attribute casting, review the Eloquent documentation.
The once
Function
The once
helper was contributed by Taylor Otwell and Nuno Maduro.
The once
helper function executes the given callback and caches the result in memory for the duration of the request. Any subsequent calls to the once
function with the same callback will return the previously cached result:
1function random(): int2{3 return once(function () {4 return random_int(1, 1000);5 });6}78random(); // 1239random(); // 123 (cached result)10random(); // 123 (cached result)
1function random(): int2{3 return once(function () {4 return random_int(1, 1000);5 });6}78random(); // 1239random(); // 123 (cached result)10random(); // 123 (cached result)
For more information on the once
helper, check out the helpers documentation.
Improved Performance When Testing With In-Memory Databases
Improved in-memory database testing performance was contributed by Anders Jenbo
Laravel 11 offers a significant speed boost when using the :memory:
SQLite database during testing. To accomplish this, Laravel now maintains a reference to PHP's PDO object and reuses it across connections, often cutting total test run time in half.
Improved Support for MariaDB
Improved support for MariaDB was contributed by Jonas Staudenmeir and Julius Kiekbusch
Laravel 11 includes improved support for MariaDB. In previous Laravel releases, you could use MariaDB via Laravel's MySQL driver. However, Laravel 11 now includes a dedicated MariaDB driver which provides better defaults for this database system.
For more information on Laravel's database drivers, check out the database documentation.
Inspecting Databases and Improved Schema Operations
Improved schema operations and database inspection was contributed by Hafez Divandari
Laravel 11 provides additional database schema operation and inspection methods, including the native modifying, renaming, and dropping of columns. Furthermore, advanced spatial types, non-default schema names, and native schema methods for manipulating tables, views, columns, indexes, and foreign keys are provided:
1use Illuminate\Support\Facades\Schema;23$tables = Schema::getTables();4$views = Schema::getViews();5$columns = Schema::getColumns('users');6$indexes = Schema::getIndexes('users');7$foreignKeys = Schema::getForeignKeys('users');
1use Illuminate\Support\Facades\Schema;23$tables = Schema::getTables();4$views = Schema::getViews();5$columns = Schema::getColumns('users');6$indexes = Schema::getIndexes('users');7$foreignKeys = Schema::getForeignKeys('users');