
Hello! It seems that the residents of Habrakhabr are still skeptical about the eloquent
phara -framework
Laravel from
Taylor Otwell . Unfortunately, no one has covered yesterday’s release of the new 5th version. I will try to fill this gap. I will not talk about what the next framework is and what bicycles are in his garage, and only highlight the moments that were affected by the change. Basically, the article will be a free translation of
release notes from the official site. I did not begin to translate a part of the concepts and terms in order not to lose the sense.
Updated design official site
I remember when I first learned about Laravel, dropped the link to off. site colleague to share their delight, but after a couple of minutes I heard the categorical "too much pink." From now on, such statements should not take place.
The new design got rid of the big pink cap at the top of the main page, leaving only a few patriotic (in the context of the framework) headlines calling for the use of this solution.
The documentation page has retained the previous look, but has become more logical in terms of the separation of versions. Previously, the development version was labeled as dev, now it is called master, which corresponds to the name of the branch on
github .
File structure
Removed
app / models folder. Now all the code regarding your application is stored in the
app folder in the
App namespace (hello psr-4!). The default namespace can be modified by Artisan with the
app: name command.
')
Controllers, middleware, and requests (a new type of class in Laravel 5.0) are grouped in the
app / Http folder, because these classes belong to the HTTP application layer. Instead of a single file with filters, each middleware (an alternative to filters, something between a request and a transition to a controller) got its own class.
The classes in the
app / Providers folder came to replace the files in the
app / start folder. These service providers provide podgruzki classes in the application, such as error handling, logging, loading routes, etc. In the same place, you can create your own service providers.
Multi-language and view files have been moved to the resources folder.
Contracts
All the main components of Laravel implement the interfaces that are in the
illuminate / contracts repository. This repository has no external dependencies. Having a convenient, centralized set of interfaces, you can use them for decoupling, and the implementation of dependencies will be an excellent alternative to facades.
DocumentationRoute cache
If your application consists solely of route-controllers, you can use the
route: cache command of the new Artisan to
significantly speed up the loading of the
route scheme. It makes sense to use this feature on applications with 100 or more routes to increase the download speed of this part of the application.
Route middleware
Laravel 4 was replaced by filters, HTTP middleware came to Laravel 5, and the familiar standard authorization filters and CSRF protection were rewritten in a new way. Middleware provides a single, permanent interface to replace any kind of filter, allowing you to conveniently examine and reject a request before it is sent to the controller for processing.
DocumentationController Method Injection
In addition to the existing constructor injection, now you can specify dependencies (orig .: type-hint dependencies) in the controller methods. The IoC container will automatically inject dependencies, even if the route contains different parameters:
public function createPost(Request $request, PostRepository $posts) {
Authentication Scaffolding
Registration, user authorization, and password recovery are now available out of the box, and their corresponding views are located in
resources / views / auth . In addition to this, the migration of the “users” table comes with the framework. The inclusion of these simple resources allows you to quickly develop prototypes, without wasting time on organizing authorization. Authorization interfaces are accessible via the
auth / login and
auth / register routes. The
App \ Services \ Auth \ Registrar service is responsible for validating and creating users.
Event objects
Now events (orig .: Events) can be defined as classes, instead of strings, for example:
class PodcastWasPurchased { public $podcast; public function __construct(Podcast $podcast) { $this->podcast = $podcast; } }
The event can be sent (original: dispatched), as before:
Event::fire(new PodcastWasPurchased($podcast));
The event handler will receive an event object, instead of a list of data:
class ReportPodcastPurchase { public function handle(PodcastWasPurchased $event) {
DocumentationCommands / Queueing
In addition to the queues (orig .: queue job format) supported in Laravel 4, Laravel 5 allows you to represent the queue as simple command objects. These commands are in the
app / Commands folder. Example:
class PurchasePodcast extends Command implements SelfHandling, ShouldBeQueued { use SerializesModels; protected $user, $podcast; public function __construct(User $user, Podcast $podcast) { $this->user = $user; $this->podcast = $podcast; } public function handle() {
The Laravel base controller uses the new
DispatchesCommands attribute (orig .: trait), which makes it quite easy to send commands to run:
$this->dispatch(new PurchasePodcastCommand($user, $podcast));
Commands can also be used for tasks that are performed synchronously (do not line up). In fact, using commands is a great opportunity to encapsulate complex tasks performed by your application.
DocumentationDatabase queue
The database queue driver is now available out of the box. Provides a simple local driver that does not require additional dependencies outside the database.
Laravel Scheduler
Previously, in order to schedule the execution of a console command, the developers had to “hang” each of them on cron, which was quite a headache. Due to the fact that this process does not occur in the application ecosystem, it was necessary to connect to the server via SSH (or via the web interface) and control the process manually. Now everything has become much easier. Task Scheduler in Laravel allows you to quickly and easily add tasks to run on a schedule within the application itself, and now it only requires one cron task!
It looks like this:
$schedule->command('artisan:command')->dailyAt('15:00');
DocumentationTinker / psysh
The
php artisan tinker command now uses Justin Hileman's Psysh (a more reliable REPL for PHP). If you enjoyed using Boris in Laravel 4, then you will definitely love Psysh. It even works under Windows! To begin, type in the terminal:
php artisan tinker
Dotenv
Instead of a confusing variety of subfolders with environment configurations, Laravel 5 uses
DotEnv from Vance Lucas. This library provides a super easy way to manage settings for a specific runtime environment and allows you to define the environment on the fly.
DocumentationLaravel elixir
Jeffrey Way's Laravel Elixir provides a simple and expressive interface for assembling resources (orig .: assets). If you were afraid to start learning Grunt or Gulp - now it will be easy. Elixir is an interlayer for Gulp to build Less, Sass, and CoffeeScript, as well as for file concatenation and version-based caching. He can even run tests!
DocumentationLaravel socialite
Laravel Socialite is an optional package that is compatible with Laravel 5.0+, which allows you to implement OAuth authentication completely painlessly. Currently supports Facebook, Twitter, Google, and GitHub. What it looks like:
public function redirectForAuth() { return Socialize::with('twitter')->redirect(); } public function getUserFromProvider() { $user = Socialize::with('twitter')->user(); }
DocumentationFlysystem integration
Now Laravel comes with the powerful file system
Flysystem , which is a level of abstraction, providing seamless integration with the local file system, Amazon S3, and Rackspace cloud storage - all in one single and elegant API! Store files on Amazon S3 is now so simple:
Storage::put('file.txt', 'contents');
DocumentationForm Requests
Laravel 5.0 presents “form requests” (original form requests) that are extended by the
Illuminate \ Foundation \ Http \ FormRequest class. These query objects can be embedded in controller methods for validating user input. An example of a simple
FormRequest :
<?php namespace App\Http\Requests; class RegisterRequest extends FormRequest { public function rules() { return [ 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:8', ]; } public function authorize() { return true; } }
After the class is defined, we can “hint” (orig .: type-hint) about it in the controller method:
public function register(RegisterRequest $request) { var_dump($request->input()); }
When Laravel IoC container determines that it is implementing an instance of the
FormRequest class, the request
will be automatically verified (orig .: validated). This means that if the controller's (original: action) method is called, you can be sure that the user input has been successfully verified according to the rules defined in
FormRequest . Even more, if the request is not valid, the redirect (which you can configure) will automatically trigger where the quick message will be sent (orig .: flash message) to the session, or by a JSON string. Form validation has never been easier!
DocumentationSimple Controller Request Validation
The base controller in Laravel 5 can now use the
ValidatesRequests attribute (orig .: trait). This tag provides a simple method for
validating an incoming request. If FormRequests is too cumbersome for your application, take a look at this:
public function createPost(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'body' => 'required', ]); }
If validation fails, an exception will be thrown and the corresponding HTTP headers sent back to the browser. Also, validation errors will “flash” (orig .: flashed) into a session. If the request was sent by AJAX, Laravel will automatically convert these errors to a JSON string.
DocumentationNew generators
On the occasion of updating the framework structure, new Artisan generator commands have been added. For more information, write in the terminal
php artisan list
Configuration cache
Now you can cache all configuration files by combining them into one file with the command:
php artisan config:cache
Symfony vardumper
The popular dd helper, which “dumps” debugging information, has been updated and now uses incredible symfony VarDumper, which provides color output and even folding / unfolding of arrays. Just try this:
dd([1, 2, 3]);
On the occasion of the release of the 5th version,
Jeffrey Way has opened
free access to video tutorials, in which it is easy to explain the new features. Until what moment is unknown. The video can be viewed online, but the download button leads to a personal account, so if you want to watch later, inspect the video player in search of a direct link.
There is also a
series of articles from
Matt Stauffer , if you like to read.
Laravel is a great, fresh framework. It accumulates in itself the best OOP practices and existing components, it is a designer, to design on which is easy and pleasant.
UPD 1: thanks
dr1v3 for
workUPD 2:
ajaxtelamonid shared a link to the translation of the
documentation of the 5th version