📜 ⬆️ ⬇️

Overview of innovations in Laravel 5.2

image

At the very beginning of this week, on Monday, the official release of Laravel 5.2 took place.
In the new version, such things as authorization, “linking” models to route and others have been improved.
In more detail under the cut.


')

Generating views for authorization


Now you can generate clean and compatible with Bootstrap views with the help of a single command for authorization, registration and password recovery of users.
php artisan make:auth 


However, it is worth using it only for new, newly created applications.

Validation of arrays


If you work with an array of form fields, now the validator in Laravel 5.2 can work with arrays!
For example, if you create more than one user at a time, then you can do this:
 $validator = Validator::make($request->all(), [ 'person.*.phone' => 'unique:users' ]); 


In this example, each of the users must have a unique phone number.

Alternatively, if you create your Request:
  public function rules() { return [ 'person.*.phone' => 'unique:users' ]; } 


Implicit "binding" of the model with the rout


No more Route :: model () !
Starting from version 5.2, you can use a simple TIP-Hint to indicate which model you need to work with:
 Route::get('/user/{user}', function (User $user) { return $user; }); 

It is important to mention that "{user}" and the name of the parameter passed to the closure must match.

Middleware grouping


Do you use more than one middleware for several of your controllers? Are you tired of listing them one at a time? No problem! Now you can group them! To do this, open the file app \ Http \ Kernel.php and you will see how you can do this. For example, by default, 2 middleware groups have already been created:
 protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ]; 


How to use it in code? Very simple:
 Route::group(['middleware' => ['web']], function () { // }); 


Request limit


Now you can set the limit of requests in one minute from one IP address:
 Route::get('/api/users', ['middleware' => 'throttle:60,1', function () { // }]); 

In this example, we limit the number of requests from one IP to 60 per minute.

Improving Global Scopes in Eloquent


In Laravel 5.2, it was possible to create custom scopes for Eloquent. This allows you to use them for each query to a specific model.

Source: https://habr.com/ru/post/274065/


All Articles