Laravel 5.0 Release new version features detailed _php tips

Source: Internet
Author: User
Tags auth documentation error handling oauth types of filters

Look forward to Laravel 5.0 has been a long time, before the jump ticket said to be released this January. Since January, has been refreshing the official website and blog, there is always no updated news, a few days ago finally saw the official website document to switch to version 5.0. The new version brings a number of exciting new features, especially the timing task queue and form request two features, just look at the update description of the simple introduction can not help but to try. Today, finally, take a moment to simply translate the official new feature documentation and hope that all friends who like the laravel frame will feel the excitement of the change. Of course, if you need a performance like Phalcon, it's definitely not what you need. If you are unable to enjoy laravel because the virtual host does not support PHP 5.4, then you will not throw away your virtual host??? Aliyun Tencent Cloud \linode ... VPS choose not to be too much ...

Laravel 5.0

Laravel 5.0 introduces a new project directory structure. The new directory structure is more conducive to using laravel to create applications. The new PSR-4 automatic loading standard has been adopted in the 5.0 edition from beginning to end. The following are the main new features of version 5.0:

Directory structure

The App/models directory in previous versions was removed entirely. Now you can put the code directly in the app directory, and all the code in that directory will be organized into the app namespace by default. This namespace can be modified with the addition of the new Artisan command app:name.

controllers, middleware and requests (Laravel 5.0) are organized into the App/http directory because they are classes associated with the Http transport layer you are applying. Unlike all the routing filters previously placed under a single filters file, all middleware (similar to the previous route filter) are now stored separately in their own class files.

A app/providers directory was added to the new version to replace the previous 4.x version of the App/start file. These service providers provide a wide range of boot methods for applications, such as error handling, logging, and routing loading. In addition, you can also create additional service providers.

The applied language files and views are moved to the resources directory.

Contracts

All the major components of Laravel implement the interfaces that are stored in the Illuminate/contracts warehouse. There are no additional dependencies for the warehouse. With such a convenient, centralized collection of interfaces, you can easily select and modify Laravel facades by decoupling and dependency injection.

For more information about contracts, you can view its full documentation.
Routing Caching

If your application is made up of a variety of controller routes (Controller routes), you can use the new Artisan command Route:cache to significantly increase the registration speed of the route. This is especially effective in applications where the number of routes is more than 100, which can greatly increase the speed of the entire application in the routing section.

Routing middleware (Route middleware)

On the basis of the 4.0-style routing "filter", the new version 5.0 already supports HTTP middleware, and the Laravel "Authentication" and "filters" have been transformed into middleware. Middleware provides a single interface for all types of filters, and you can easily review and reject requests.

For more information about the middleware, you can view its full documentation.

Controller Method Injection

In addition to the existing constructor injection, you can also type constrain the dependencies in the Controller method in the new version. IoC container automatically injects dependencies even when the route contains other parameters.

Copy Code code as follows:

Public Function Createpost (Request $request, Postrepository $posts)
{
//
}

Certified Scaffolding

User registration, authentication and password reset controllers are already built into the 5.0 version of the Web site framework, in addition to the controller, there are simple views, stored in the Resources/views/auth directory. In addition, the site's initial framework contains a migration file for the "Users" table. These simple resources help developers not to spend a lot of time on user authentication features. Certification-related pages can be accessed through the two routes of Auth/login and Auth/register. The App\services\auth\registrar service is responsible for creating and authenticating users.

Event Object

In the new version, you can define events as objects rather than strings. Look at the following example:

Copy Code code as follows:

Class Podcastwaspurchased {

Public $podcast;

Public function __construct (Podcast $podcast)
{
$this->podcast = $podcast;
}

}

This event can be invoked like this:

Event::fire (New podcastwaspurchased ($podcast));
Of course, your event handler receives no longer a list of data, but an event object:

Copy Code code as follows:

Class Reportpodcastpurchase {

Public function handle (podcastwaspurchased $event)
{
//
}

}

For more information about events, you can view the full documentation for this event.

Command/queue

On the basis of the task queues supported by version 4.0, 5.0 supports defining a task queue as a simple command object. These commands are stored in the App/commands directory. The following is a simple example of a command:

Copy Code code as follows:

Class Purchasepodcast extends Command implements Selfhandling, shouldbequeued {

Use Serializesmodels;

protected $user, $podcast;

/**
* Create a new command instance
*
* @return void
*/
Public function __construct (User $user, Podcast $podcast)
{
$this->user = $user;
$this->podcast = $podcast;
}

/**
* Execute command
*
* @return void
*/
Public function handle ()
{
Handling the logic of purchasing podcast videos

Event (New podcastwaspurchased ($this->user, $this->podcast));
}

}

The Laravel basic controller (base controller) uses a new Dispatchescommands feature that allows you to easily monitor the execution of commands:

$this->dispatch (New Purchasepodcastcommand ($user, $podcast));
Of course, you can use commands not only for task queues (asynchronous execution), but also for synchronization tasks. In fact, it's a good choice to encapsulate the complex tasks that your application needs to perform as commands. For more information about commands, you can view the detailed documentation for the command bridge.

Database queues

The new laravel contains a database queue driver that provides a simple, local queue driver without installing additional packages. (for example, to allow a database that does not support transactions to perform a transaction-like data operation)

Laravel timed Task

In the past, in order to perform a console task regularly, developers had to rely on Cron tasks. This brings a lot of inconvenience. Because scheduled tasks are not included in the source code of the site, they must be logged into the server via SSH to add Cron tasks. The new Laravel timing task allows developers to define timed commands within the Laravel framework, and then only need to define a total Cron task on the server.

Like what:

Copy Code code as follows:

$schedule->command (' Artisan:command ')->dailyat (' 15:00 ');

Similarly, for more information about timed tasks, you can consult the full documentation.

Tinker/psysh

The PHP Artisan Tinker command is in the new version with the help of Justin Heleman developed Psysh. If you like the Boris in Laravel 4.0, you will definitely like Psysh. Boris does not run well under Windows, Psysh fully supports windows! Use the same method as before:

Copy Code code as follows:

PHP Artisan Tinker

dotenv

In Laravel 5.0, the DOTENV, which was implemented with Vance Lucas, replaces the nested structure in the previous version and makes it confusing for the environment configuration directory. This framework provides a very simple way to manage the configuration of your environment. It is easy to detect and differentiate between different operating environments in Laravel 5.0. For more information, please visit the full configuration document.

Laravel Elixir

The Laravel elixir provided by Jeffrey Way provides a straightforward merge that compiles interfaces for resource files. If you've ever felt a big head about configuring grunt or GULP, now you're liberated. Elixir allows you to easily compile your less, Sass, and Coffeescript files with the help of Gulp. It can even perform tests for you.

For more information about elixir, please visit the full document.

Laravel socialite

Laravel socialite is compatible with only the Laravel 5.0 version of the optional package, it provides a complete and hands-on OAuth solution without difficulty. Currently, socialite supports Facebook, Twitter, Google and Github. It looks like this:

Copy Code code as follows:

Public Function Redirectforauth ()
{
return Socialize::with (' Twitter ')->redirect ();
}

Public Function Getuserfromprovider ()
{
$user = Socialize::with (' Twitter ')->user ();
}

So you don't have to spend a lot of time writing OAuth certification process, minutes easy to handle. The full document contains all the details about the optional package.

Flysystem Integration

The new Laravel also contains a powerful Flysystem file processing static library. With this library, developers can easily use a fully consistent API to implement local, Amazon S3 or Rackspace file storage. For example, storing a file in Amazon S3 can be as simple as this:

Copy Code code as follows:

Storage::p ut (' file.txt ', ' contents ');

For more details on Laravel flysystem integration, you can view its full documentation

Form Request

Laravel 5.0 brings a new form requests, which extends from the Illuminate\foundation\http\formrequest class. These request objects can be combined with the Controller method injection to provide a new way to validate user input. Simply cite an example of a formrequest:

Copy Code code as follows:

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 defining the corresponding Formrequest extension class, you can get a type hint in the Controller method:
Copy Code code as follows:

Public Function Register (registerrequest $request)
{
Var_dump ($request->input ());
}

When the Laravel IoC container recognizes the type of the method variable, it automatically injects the Formrequest instance, and the request is automatically validated. This means that when your controller is invoked, you can safely use the input data that is contained in the request because they have been validated by the rules that you specify in the form request class. Not only that, if the request validation fails, the system is automatically redirected to your predefined route and contains information about the error (write session as needed, or convert to JSON format.) Form validation has never been simpler. For more details on formrequest validation, consult the documentation.

Controller Request Simple Authentication

The Controller base class for Laravel 5.0 also contains a validatesrequests trait. The trait provides a simple validate method for validating requests. If Formrequests is too heavy for your application, you can use this lightweight version of:

Copy Code code as follows:

Public Function Createpost (Request $request)
{
$this->validate ($request, [
' title ' => ' required|max:255 ',
' Body ' => ' required ',
]);
}

If the validation fails, the system throws an exception, and the corresponding HTTP request is automatically sent to the browser. Validation errors are also written to the session. If the request was initiated in AJAX mode, Larave will automatically send a JSON-form validation error message.

For more details on formrequest validation, consult the documentation.

A new generator

To facilitate the generation of a new default application structure, the new Artisan build command has been added to the framework. You can view detailed commands through the PHP artisan list.

Configuring caching

With the Config:cache command, all configuration items can be written to a cached file.

Symfony Vardumper

Auxiliary method for debugging output variable information DD, upgraded in a new version, uses a powerful Symfony vardumper. It can output debugging information with color highlighting and array folding functions. You can try:

Copy Code code as follows:

DD ([1, 2, 3]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.