The latest version of Laravel is 11! The latest major updates and features are listed here.

Posted by

A slimmer application skeleton

It was time to simplify and rethink the structure of a Laravel application with the release of Laravel 11. The objective? to provide you with a more streamlined and effective foundation for your endeavours. And they have delivered, I assure you.

The all-new bootstrap/app.php file

Reviving the bootstrap/app.php file to serve as your primary command centre is the centrepiece of this makeover. You may adjust exception handling, middleware, application routeing, service providers, and a tonne of other features from this one location. Consider it as the spaceship of the captain.

Simplified service providers

Do you have experience managing several service providers? Says Laravel 11: “No more!” There is currently only one AppServiceProvider. This update cleans up your coding by combining the features of the previous service providers into the AppServiceProvider or bootstrap/app.php.

Optional API and broadcast route files

Not all apps require broadcasting and API functionality right away. In recognition of this, Laravel 11 does not by default include the api.php and channels.php route files. Require them? Easy as a single Artisan command. Here is where Laravel’s flexibility really comes into play, enabling you to add features only when needed.

php artisan install:api
php artisan install:broadcasting

Gone are the default middleware classes

The days of a disorganised middleware folder are long gone. You may now enjoy a cleaner application structure without sacrificing the flexibility to customise middleware behaviour from bootstrap/app.php thanks to Laravel 11’s migration of these middleware inside the framework itself. Find out more: (How to alter Laravel 11’s default middleware)[/customize-laravel-11-middleware]

->withMiddleware(function (Middleware $middleware) {
    $middleware->redirectGuestsTo('/admin/login');
})

Exception handling also moved to bootstrap/app.php

Exception handling has likewise relocated to the comfortable boundaries of bootstrap/app.php in the spirit of consolidation. This maintains the structure of your application minimal and eliminates the need for you to search through several files in order to manage exceptions.

Hereā€™s a code sample from Laravel 11ā€™s release notes (bootstrap/app.php)

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->dontReport(MissedFlightException::class);
 
    $exceptions->reportable(function (InvalidOrderException $e) {
        // ...
    });
})

Direct tasks scheduling in routes/console.php

Scheduling tasks is now as simple as adding a few lines to your routes/console.php file, thanks to the new Schedule facade. No need for a console kernel anymore.

In routes/console.php:

use Illuminate\Support\Facades\Schedule;
 
Schedule::command('some-service:sync')->daily();

A minimalist base controller class

The base controller class in Laravel 11 has gone on a diet. The AuthorizesRequests and ValidatesRequests traits still exist, but you will also now have to opt-in.

For instance, if you are using policies and want to do check for authorizations, youā€™ll have to include the AuthorizesRequests trait in your base controller (app/Http/Controllers/Controller.php):

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller
{
    use AuthorizesRequests;
}

The new health-check endpoint

Laravel 11 introduces a new health-check endpoint that allows you to perform various verifications of the different parts of your application and ensure everything is running smoothly.

The endpoint can be defined in bootstrap/app.php like so:

->withRouting(
    web: __DIR__.'/../routes/web.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/up',
)

Whenever the endpoint is hit, a Illuminate\Foundation\Events\DiagnosingHealth event is dispatched.

Per-second rate limiting

Rate limiting in Laravel is incredibly easy to set up. With Laravel 11, you can now set a rate limit per second.

But why is it useful? Letā€™s take this example:

RateLimiter::for('invoices', function (Request $request) {
    return Limit::perMinute(120); // [tl! --]
    return Limit::perSecond(2); // [tl! ++]
});
  1. If we limit the amount of requests per minute, it means that your users will also be able to send 120 requests is a second.
  2. But if we limit the amount of requests per second, your users wonā€™t be able to cram 120 requests in a second, while still being limited to 120 requests per minute.

Discover the new Model::casts() method

Usually, in Laravel, you declare attribute casting in an Eloquent model like this:

class User extends Model
{
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

With Laravel 11, you can now define your casting through a casts() method in your model, giving you a chance to use static methods with parameters. This is how it looks:

class User extends Model
{
    protected function casts() : array
    {
        return [
            'foo' => AsCollection::using(FooCollection::class),
        ];
    }
}

Moreover, you can now also specify your casts as an array:

class User extends Model
{
	// Even in the old $casts property!
    protected $casts = [
        'foo' => [AsCollection::class, FooCollection::class],
    ];
  
    protected function casts() : array
    {
        return [
            'foo' => [AsCollection::class, FooCollection::class],
        ];
    }
}

Note that the casts() method is prioritized over the $casts property.

All these changes are non-breaking, meaning they wonā€™t affect your current code if you update to Laravel 11.

See the pull request on GitHub: [11.x] Adds Model::casts() method and named static methods for built-in casters

Thereā€™s a new handy trait named ā€œDumpableā€

This pull request introduces a new Dumpable trait in Laravel 11, intended to replace the current dd and dump methods in most of the frameworkā€™s classes.

The trait allows Laravel users and package authors to include debugging methods easily within their classes by utilizing this trait.

Hereā€™s a code example showing how it can be used:

<?php

namespace App\ValueObjects;

use Illuminate\Support\Traits\Dumpable;
use Illuminate\Support\Traits\Conditionable;

class Address
{
    use Conditionable, Dumpable;

    // ...
}

$address = new Address;

// Before: 
$address->foo()->bar();

// After:
$address->foo()->dd()->bar();

See the pull request on GitHub: [11.x] Adds Dumpable concern

Dropped support for PHP 8.1

PHP 8.2 is established and PHP 8.3 is now the latest version of PHP. Laravel can now move forward and abandon 8.1.

But remember: your Laravel apps donā€™t need to be updated to the latest and greatest as soon as theyā€™re released.

Especially if you have projects with paid clients or employees who depend on them to do their work.

Those projects need to slowly but surely move forward by doing extensive testing. Donā€™t rush.

See the pull request on GitHub: [11.x] Drop PHP 8.1 support

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x