blog imageMarch 13, 2024
blog imageBy trreta

Whats new in Laravel 11

Whats new in Laravel 11

Share this article

facebooktwitterlinkedin

Whats new in Laravel 11


Laravel 11 brings forth a minimalist application structure designed specifically for new Laravel applications. This update aims to offer a leaner and more contemporary experience, all the while ensuring compatibility with existing applications. Despite the streamlined approach, the new structure maintains familiarity with Laravel's core concepts, ensuring a smooth transition for developers


Upgrading to Laravel 11 is a smart move for a few reasons:

 

1. Faster Performance - Your apps will run faster because of improvements in how routes are handled, how queries are made, and how data is cached.
2. Better Security - Laravel 11 includes new security features and fixes that help keep your app and its data safe.

3. New Tools for Developers - There are better tools for debugging, new commands for Artisan (Laravel's command-line tool), and updated documentation to help you work more efficiently.

4. Easy to Upgrade - Moving from Laravel 10 to 11 is straightforward because the new version is designed to work well with the older one.

By updating to Laravel 11, you can take advantage of the latest PHP 8.2 features, easier setup, and better testing tools. This means you can make stronger and safer apps more quickly.

 

What’s New in Laravel 11: Upcoming Changes

1. Improved Routing Efficiency

One of the first things you’ll notice in Laravel 11 is the improved routing efficiency. The team has worked on making the routing process faster and more intuitive. For example, you can now define route groups more succinctly.

Route::prefix('admin')->group(function () {

    Route::get('/dashboard', function () {

        // Dashboard view

    });

    Route::get('/profile', function () {

        // Profile view

    });

});

This might look familiar, but under the hood, Laravel 11 optimizes these routes, making your application a tad quicker. It’s like giving your routes a little caffeine boost!

2. A slimmer application skeleton

With the introduction of Laravel 11, it was time to declutter and redefine how a Laravel application is structured. The goal? To give you a leaner and more efficient starting point for your projects. And let me tell you, they’ve delivered.

3. The all-new bootstrap/app.php file

The heart of this makeover is the bootstrap/app.php file, revitalized to act as your central command station. It’s here you’ll tweak application routing, middleware, service providers, exception handling, and a bunch more – all from one spot. Think of it as the captain’s a spaceship.

4. Simplified service providers

Are you used to juggling multiple service providers? Laravel 11 says, “No more!” Now, there’s just one AppServiceProvider. This change consolidates the functions of the old service providers into either the bootstrap/app.php or the AppServiceProvider, making your codebase tidier.

5. Optional API and broadcast route files

Not every app needs API and broadcasting capabilities out of the gate. Laravel 11 acknowledges this by not including API.php and channels.php route files by default. Need them? Just a simple Artisan command away. Laravel’s flexibility shines here, allowing you to add features only when you need them.

php artisan install:api

php artisan install:broadcasting
6. Gone are the default middleware classes

Gone are the days of a cluttered middleware folder. Laravel 11 has moved these middleware into the framework itself, letting you enjoy a cleaner application structure without losing the ability to customize middleware behavior from bootstrap/app.php.

->withMiddleware(function (Middleware $middleware) {

   $middleware->redirectGuestsTo('/admin/login');

})

7. Exception handling also moved to bootstrap/app.php

In the spirit of consolidation, exception handling has also moved to the cozy confines of bootstrap/app.php. This keeps your application’s structure lean and means you won’t have to hunt through multiple files to manage exceptions.

->withExceptions(function (Exceptions $exceptions) {

   $exceptions->dontReport(MissedFlightException::class);

   $exceptions->reportable(function (InvalidOrderException $e) {

       // ...

   });

})

8. 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();

9. 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 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;

}

10 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, an Illuminate\Foundation\Events\DiagnosingHealth event is dispatched.

11. Per-second rate limiting

In Laravel is incredibly easy to set up. With Laravel 11, you can now set a rate limit per second. To be honest, while this is a nice touch, I have yet to understand why this change was needed.

If that’s obvious to you, please share it in the comments below, we’d all love to learn more!

RateLimiter::for('invoices', function (Request $request) {

   return Limit::perMinute(120);

   return Limit::perSecond(2);

});

12. 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.

13 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();

14. 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.

Improvements in Laravel 11 That Make Web Development Easier

 

Laravel 11 is designed to make web development more manageable and more accessible. The new features and updates to the framework are meant to make typical development jobs more manageable, increasing productivity and efficiency.

With Laravel 11, there is less initial setup code, a significant change that lets developers focus on making their projects more unique. This change saves time and makes the framework convenient to use, especially for people new to Laravel or web development in general.

Laravel 11 makes it a cakewalk to work with databases by giving you better ways to handle data. With this improvement, you'll spend less time setting up how data interacts and more time building the core features of apps.

With the new Dumpable trait in Laravel 11, developers can find bugs in their code quickly and easily because it has better testing features. This feature is vital for keeping the standard and performance of apps high, which makes the development process handy.

How to Be a Part of Laravel 11's Growth?

Laravel's growth and evolution are significantly influenced by its community. Being a part of this growth is not only rewarding but also contributes to the improvement of the framework. Here’s how you can get involved:

  • If you're a developer, consider contributing to Laravel 11's codebase. You can submit pull requests on GitHub for new features or bug fixes. Before contributing, familiarize yourself with Laravel's contribution guidelines.
  • If you encounter bugs or issues while using Laravel 11, report them on the Laravel GitHub repository. This feedback is crucial for the continuous improvement of the framework.
  • If you have ideas for new features or improvements, share them through the Laravel forums or GitHub discussions. Your input could shape the future of Laravel.

Conclusion

Laravel 11 streamlines project setup and enhances data handling, catering to both seasoned developers and newcomers. Aligned with PHP 8.2 updates, it accelerates debugging processes, fostering simplicity and efficiency in development workflows.

Let's shape technology around your digital needs!

If you are curious to talk to Trreta Techlabs and know more about our products and services, feel free to reach out!