Laravel 11 – Overview of changes Part 3

Welcome to the last part of Laravel 11 – Overview of changes.

Route changes

From now on, in the /routes folder, we will find only web.php and console.php routes. The api.php and broadcast.php routes can be optionally installed using new commands: php artisan install:api and php artisan install:broadcast. Installing the API will also install Laravel/Sanctum and prepare the project for writing RESTful APIs. Similarly, if there is a need to use WebSockets, we can install broadcast, and the project will be automatically configured for it.

The console.php file is now used for an additional purpose. One of the changes in the project was the removal of Kernel files from the /app folder. Previously, we could use Console Kernel to define regular tasks in the project by specifying how often a particular command should run. Now, this logic has been moved to the console routes. Starting from Laravel 11, we will define in console routes how often cron will run specific commands.

Health route

A new web route /up has been added, which returns information about whether the application received a request and how long it took for the application to render a response. It can be disabled by commenting it out in bootstrap/app.php. 

New helper once()

A new helper function once() has been added. The once() function ensures that the method called within it always returns the same result. To generate a new result, we must utilize the Once::flush() function.

function randomInt(): int
{
   return once(function () {
       return random_int(1, 100);
   });
}

randomInt(); // 97
randomInt(); // 97 - cached value
randomInt(); // 97 - cached value

Once::flush();

randomInt(); // 5
randomInt(); // 5 - cached value