1. Slim Skeleton
Laravel 11 introduced a much-anticipated change: the slim application skeleton. This revamp focuses on streamlining the application structure and configuration process, making development more efficient and enjoyable. Let’s delve into the key aspects of the slim skeleton
When you install a new Laravel project, the folder structure will look like this:
app/
├── Http/
│ └── Controllers/
│ └── Controller.php
├── Models/
│ └── User.php
└── Providers/
└── AppServiceProvider.php
bootstrap/
├── app.php
└── providers.php
config
…
Previously, scattered configuration files populated the application structure. Laravel 11 consolidates this. Here’s an example of registering a service provider in bootstrap/app.php
:
php
use Illuminate\Support\ServiceProvider;
class UserServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(UserService::class, function ($app) {
return new UserService($app->make(UserRepository::class));
});
}
}
2.Config Files
By default in Laravel 11, you will see empty config folders. If you want to change the configuration, you can do it using the .env file. However, if you need to make changes to config files, you need to publish them using an artisan command.
Laravel 11 provides the following commands to publish config files.
Laravel Publish Single Config File:
You can run the following command to publish a single config file:
php artisan config:publish
Now, you will have list of config files, choose one and it:
Laravel Publish All Config Files:
You can run the following command to publish all configuration files:
php artisan config:publish –all
3.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();
4.Helth-check endpoint
Laravel 11 introduces a new 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.
5.Dumpable Trait
Laravel 11 introduces a new Dumpable
trait. It is intended to replace the current dd()
and dump()
methods in most of the framework’s classes. Users and package authors can include this trait for easier code debugging.
Here is the usage example:
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Dumpable;
class Address{
use Conditionable, Dumpable;
// …
}
$address = new Address;
$address->setThis()->setThat();
$address->setThis()->dd()->setThat();
6.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. Laravel’s flexibility shines here, allowing you to add features only when you need them.
php artisan install:api
php artisan install:broadcasting
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. Artisan’s New Toy: make:enum
Command
Laravel 11’s Artisan CLI offers a make:enum
command for swift enum creation. Here’s an example:
Bash
php artisan make:enum OrderStatus pending,processing,shipped,cancelled
This generates an OrderStatus
enum class with the specified options.
8. Cleaner Code: The once
Method
The once
method helps avoid redundant operations on collections. Here’s an example:
$users = User::all();
$processedUsers = $users->once(function ($user) {
// Perform some processing on the user (only once per unique user)
return $user->process();
});
This ensures the processing logic executes only once for each unique user in the collection.
By incorporating these new features and exploring the Laravel documentation, you can leverage the power of Laravel 11 to build modern web applications efficiently. Remember, this is just a glimpse into the exciting world of Laravel 11. Keep exploring