This article mainly introduces Laravel 5.0 releases new version features in detail. This article explains the directory structure, Contracts, routing cache, routing intermediate, New features such as controller method injection and authentication scaffolding, friends in need can refer to it
Translation Note: I have been looking forward to Laravel 5.0 for a long time. It was previously delayed and said it would be released in January this year. I have been refreshing the official website and blog since January, but there has been no update news. I finally saw the official website document switch a few days ago. It’s version 5.0. The new version brings many exciting new features, especially the scheduled task queue and form request features. Just looking at the brief introduction in the update notes makes me want to try it out. I finally took it out today. It took me a while to briefly translate the official new feature description document. I hope that all friends who like the Laravel framework can feel the exciting changes brought by this version. Of course, if you need Phalcon-like performance, then it It's definitely not what you need. If you can't enjoy Laravel because the virtual host doesn't support PHP 5.4, then why don't you throw away your virtual host??? Alibaba Cloud Tencent Cloud Linode... Don't have too many VPS choices... .
Laravel 5.0
Laravel 5.0 introduces a new project directory structure. The new directory structure is more conducive to creating applications with Laravel. Version 5.0 adopts the new PSR-4 auto-loading standard from beginning to end. The following are the main new features of version 5.0 Features:
Directory structure
The app/models directory in previous versions has been completely removed. Now you can place the code directly in the app directory, and all code in this directory will be organized into the app namespace by default. This namespace can be accessed through the new Added Artisan command app:name to modify.
Controllers, middleware and requests (a new class added in Laravel 5.0) are organized into the app/Http directory because they are all classes related to the HTTP transport layer of your application. Unlike before, all routing filters were put into a single filters file, now all middleware (similar to the previous route filter) are stored in their own class files.
The new version adds an app/Providers directory to replace the app/start file of the previous 4.x version. These service providers provide various boot methods for applications, such as error handling, logging, route loading, etc. . In addition, of course you can also create additional service providers.
The application’s language files and views have been moved to the resources directory.
Contracts
All major components of Laravel implement interfaces stored in the illuminate/contracts repository. This repository has no additional dependencies. With such a convenient, centrally located set of interfaces, you can easily Make choices and modifications to Laravel Facades in terms of decoupling and dependency injection.
To learn more about contracts, you can view its full documentation.
Route cache
If your application consists of various controller routes, you can use the new Artisan command route:cache to greatly improve the registration speed of routes. This is useful for applications with more than 100 routes. It is particularly effective and can greatly improve the speed of the entire application in the routing part.
Route Middleware
Based on the version 4.0 style routing "filters", the new version 5.0 already supports HTTP middleware. Laravel's own "authentication" and "filters" have been converted into middleware. Middleware is for all types of filters. Providing a single interface, you can easily review and reject requests.
To learn more about the middleware, you can view its full documentation.
Controller method injection
In addition to the existing constructor injection, in the new version you can also type constraints on dependencies in controller methods. The IoC container will automatically inject dependencies, even when the route contains other parameters.
The code is as follows:
User registration, authentication and password reset controllers have been built into the website framework of version 5.0. In addition to controllers, there are also simple views stored in the resources/views/auth directory. In addition, the website initialization The framework also contains a migration file for the "users" table. These simple resources help developers avoid spending a lot of time on user authentication functions. Authentication-related pages can be accessed through the two routes auth/login and auth/register. The AppServicesAuthRegistrar service handles creating and authenticating users.
Event Object
In the new version, you can define events as objects instead of strings. See the example below:
The code is as follows:
Event::fire(new PodcastWasPurchased($podcast));
Of course, what your event handler receives is no longer a data list, but an event object:
The code is as follows:
Command/Queue
Based on the task queue supported by version 4.0, 5.0 supports defining task queue as a simple command object. These commands are stored in the app/Commands directory. The following is a simple command example:
The code is as follows:
$this->dispatch(new PurchasePodcastCommand($user, $podcast));
Of course, you can use commands not only for task queues (asynchronous execution), but also for synchronous tasks. In fact, it is a good choice to encapsulate the complex tasks that your application needs to perform into commands. Learn about commands For more information, you can view the detailed documentation of the command bridge.
Database Queue
The new version of Laravel includes a database queue driver, providing a simple, local queue driver without the need to install additional packages. (Annotation: For example, allowing a database that does not support transactions to perform transaction-like data operations)
Laravel scheduled tasks
In the past, in order to regularly execute console tasks, developers had to rely on Cron tasks. This caused great inconvenience. Because scheduled tasks were not included in the source code of the website, and you had to log in to the server through SSH to add Cron Tasks. The new version of Laravel's scheduled tasks allow developers to define scheduled execution commands within the Laravel framework, and then only need to define a total Cron task on the server.
For example:
The code is as follows:
Tinker / Psysh
The php artisan tinker command uses Psysh developed by Justin Heleman in the new version. If you like Boris in Laravel 4.0, you will definitely like Psysh. Boris does not run well under Windows, and Psysh fully supports Windows! How to use Same as before:
The code is as follows:
In Laravel 5.0, DotEnv implemented by Vance Lucas replaces the nested structure and confusing environment configuration directory in previous versions. This framework provides a very simple way to manage environment configuration. In Laravel Detecting and differentiating different runtime environments is a breeze in 5.0. For more details, visit the full configuration documentation.
Laravel Elixir
Laravel Elixir provided by Jeffrey Way provides a concise and easy-to-understand interface for merging and compiling resource files. If you have ever felt overwhelmed by configuring Grunt or Gulp, now you are liberated. Elixir allows you to easily Compile your Less, Sass and CoffeeScript files with Gulp. It can even run the tests for you.
Learn more about Elixir, please visit the full documentation.
Laravel Socialite
Laravel Socialite is only compatible with the optional package of Laravel 5.0 or above, which provides a complete and easy-to-use OAuth solution. Currently, Socialite supports Facebook, Twitter, Google and Github. It looks like this:
The code is as follows:
Flysystem integration
The new version of Laravel also includes the powerful Flysystem file processing static library. Through this library, developers can easily get started and use exactly the same API to implement local, Amazon S3 or Rackspace file storage. For example, store a file in Amazon S3 The file can be as simple as this:
The code is as follows:
Form request
Laravel 5.0 brings new form requests, which extends from the IlluminateFoundationHttpFormRequest class. These request objects can be combined with controller method injection to provide a new method of validating user input. Here is a simple example of FormRequest:
The code is as follows:
The code is as follows:
Controller requests simple verification
The controller base class of Laravel 5.0 also contains a ValidatesRequests trait. This trait provides a simple validate method for validating requests. If FormRequests is too heavy for your application, you can use this Lightweight version:
The code is as follows:
For more details about FormRequest validation, please consult the documentation.
Brand new generator
In order to facilitate the generation of new default application structures, new Artisan generation commands have been added to the framework. You can view detailed commands through php artisan list.
Configuration Cache
Through the config:cache command, all configuration items can be written into a cache file.
Symfony VarDumper
The auxiliary method dd for outputting variable information for debugging has been upgraded in the new version, using the powerful Symfony VarDumper. It can output debugging information with color highlighting and array folding functions. You can try it:
Copy the code The code is as follows: