What Is New in Laravel 5.2
Laravel 5.2 introduces a suite of powerful features focused on developer efficiency and application structure. This release builds on the foundation of 5.1 with more robust tools for authentication and background job processing.
| Category | Key Changes |
|---|---|
| New Features | Implicit Model Binding, API Rate Limiting, Authentication Scaffolding, Multiple Authentication Guards |
| Improvements | Simplified Auth Driver, Array Validation Improvements, Eloquent Global Scope API |
| Deprecated | Illuminate\Contracts\Bus\SelfHandling Contract |
How does authentication work in Laravel 5.2?
Laravel 5.2 completely overhauls authentication with built-in support for multiple guards. This allows you to define separate authentication systems for different user types, like admins and customers, within a single application.
The framework now includes scaffolding for authentication views and logic out of the box. In practice, this means you can run php artisan make:auth to generate login, registration, and password reset functionality instantly.
What are the new implicit model bindings?
Implicit model binding automatically injects model instances into your routes and controllers. You type-hint a route parameter, and Laravel fetches the corresponding Eloquent model, reducing boilerplate code.
For example, a route parameter {user} will automatically be resolved to a User model instance. This cleans up your controller methods significantly, as you no longer need manual User::findOrFail($id) calls.
How is API rate limiting implemented?
Laravel 5.2 introduces a middleware for throttling API requests. You can easily limit the number of requests a user can make to a given route within a specified number of minutes.
This is implemented using the throttle middleware. You simply chain it to your route definition, specifying the maximum attempts and the decay minutes, which is crucial for building public-facing APIs.
Route::group(['middleware' => 'throttle:60,1'], function () {
// Routes limited to 60 requests per minute
});
What improvements were made to Eloquent?
The global scope API was refined, allowing for a more fluent and manageable way to add constraints to all queries for a model. You can now define scopes using a dedicated class, making them easier to maintain and understand.
This matters because it encapsulates query logic, keeping your model clean. You can easily push and remove global scopes as needed, providing more control over your query building process.
What changed with array validation?
Validating array form inputs became much more intuitive. The validator now allows you to use dot notation to specify validation rules for nested array elements, streamlining the process of handling complex form data.
For instance, validating a field like person[][name] is straightforward. This eliminates the need for cumbersome custom validation logic for nested arrays, which was a common pain point.
FAQ
Is the SelfHandling contract still required in 5.2?
No, it has been deprecated. In Laravel 5.2, you can place your job's handler class within the job class itself using the handle method. The contract is no longer necessary and will be removed in future versions.
How do I use the new authentication scaffolding?
Run the Artisan command php artisan make:auth. This will generate the views and routes for login, registration, and password reset, providing a complete starting point for your app's auth system.
Can I use multiple auth guards simultaneously?
Yes, that's a core feature of 5.2. You can define multiple guards in your config/auth.php file (e.g., 'web', 'api', 'admin') and specify which guard to use for authentication throughout your application.
What happens if implicit model binding fails?
If a matching model instance is not found for a given route parameter, Laravel will automatically return a 404 HTTP response, just like the old Model::findOrFail() method would.
How do the array validation improvements work?
You can now validate nested array inputs using dot notation. For example, to validate a field named users.*.email, your rule would be 'users.*.email' => 'required|email', which applies to every email in the users array.