Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the features of the new version of Laravel 5.0

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the features of the new version of Laravel 5.0". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the features of the new version of Laravel 5.0".

Laravel 5.0

Laravel 5.0 introduces a new project directory structure. The new directory structure is more convenient to use Laravel to create applications. Version 5. 0 adopts the new PSR-4 automatic loading standard from beginning to end. The following are the major new features of version 5.0:

Directory structure

The app/models directory in the previous version was completely removed. Now you can put the code directly in the app directory, where all code is organized into the app namespace by default. This namespace can be modified with the newly added Artisan command app:name.

Controllers, middleware, and requests (a new class 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. Instead of putting all route filters under a single filters file, all middleware (similar to the previous route filter) are now stored in their own class files.

An app/Providers directory has been added to the new version to replace the previous 4.x app/start file. These service providers provide a variety of boot methods for applications, such as error handling, logging, routing loading, etc. In addition, of course you can create additional service providers.

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

Contracts

All major components of Laravel implement interfaces stored in illuminate/contracts repositories. The warehouse has no additional dependencies. With such a convenient, centrally stored set of interfaces, you can easily choose and modify Laravel Facades in terms of decoupling and dependency injection.

To learn more about contracts, you can view its complete documentation.

Routing cach

If your application consists of a variety of controller routing (controller routes), you can use the new Artisan command route:cache to greatly increase the speed of routing registration. This is especially effective in applications with more than 100 routes, which can greatly improve the speed of the whole application in the routing part.

Routing Middleware (Route Middleware)

Based on the 4.0 style routing "filter", the new version 5.0 already supports HTTP middleware, and Laravel's "authentication" and "filters" have been converted into middleware. Middleware provides a single interface for all types of filters, and you can easily review and reject requests.

To learn more about middleware, you can view its complete documentation.

Controller method injection

In addition to the existing constructor injection, type constraints can also be imposed on dependencies in controller methods in the new version. IoC container automatically injects dependencies, even when the route contains other parameters.

The copy code is as follows:

Public function createPost (Request $request, PostRepository $posts)

{

/ /

}

Certified scaffolding

User registration, authentication and password reset controllers have been built into the version 5.0 website framework, in addition to the controller, there are simple views, stored in the resources/views/auth directory. In addition, the initial framework of the site also contains a migration file of the "users" table. These simple resources help developers not to spend a lot of time on user authentication. Authentication-related pages can be accessed via auth/login and auth/register routes. The App\ Services\ Auth\ Registrar service handles the creation and authentication of users.

Event object

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

The copy code is as follows:

Class PodcastWasPurchased {

Public $podcast

Public function _ _ construct (Podcast $podcast)

{

$this- > podcast = $podcast

}

}

This event can be called like this:

Event::fire (new PodcastWasPurchased ($podcast))

Of course, what your event handler receives is no longer a data list, but an event object:

The copy code is as follows:

Class ReportPodcastPurchase {

Public function handle (PodcastWasPurchased $event)

{

/ /

}

}

For more information about the event, you can view its complete documentation.

Command / queue

Based on the task queue supported by version 4.0, 5.0 supports defining the task queue as a simple command object. These commands are stored in the app/Commands directory. Here is an example of a simple command:

The copy code is 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 orders

*

* @ return void

, /

Public function handle ()

{

/ / deal with the logic of purchasing podcast videos

Event (new PodcastWasPurchased ($this- > user, $this- > podcast))

}

}

Laravel's basic controller (base controller) uses the new DispatchesCommands feature, which 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 synchronized tasks. In fact, encapsulating complex tasks that your application needs to perform into commands is a good choice. For more information about commands, you can view the detailed documentation of the command bridge.

Database queue

The new version of Laravel includes database queue driver, which provides a simple, local queue driver without the need to install additional packages. (translation note: for example, let a database that does not support transactions perform transaction-like data operations)

Laravel scheduled task

In the past, in order to perform console tasks regularly, developers had to rely on Cron tasks. This brings great inconvenience. Because scheduled tasks are not included in the source code of the site, and you must log in to the server through SSH to add Cron tasks. The new version of Laravel's scheduled tasks allow developers to define scheduled commands within the Laravel framework, and then only need to define a total Cron task on the server.

For example:

The copy code is as follows:

$schedule- > command ('artisan:command')-> dailyAt (' 15glaze 00')

Similarly, for more information about scheduled tasks, you can refer to the complete documentation.

Tinker / Psysh

In the new version, the php artisan tinker command is developed by Psysh. Justin Heleman. 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! Use it the same way as before:

The copy code is as follows:

Php artisan tinker

DotEnv

In Laravel 5.0, DotEnv implemented with Vance Lucas replaces the nesting structure in the previous version, which is easy to confuse the environment configuration directory. This framework provides a very simple way to manage the configuration of the environment. It is easy to detect and distinguish different operating environments in Laravel 5.0. For more details, please visit the complete configuration documentation.

Laravel Elixir

Laravel Elixir provided by Jeffrey Way provides an easy-to-understand interface for merging and compiling resource files. If you've ever felt big about configuring Grunt or Gulp, now you're liberated. Elixir allows you to easily compile your Less, Sass and CoffeeScript files with Gulp. It can even perform tests for you.

For more details on Elixir, please visit the complete documentation.

Laravel Socialite

Laravel Socialite is only compatible with optional packages above Laravel 5.0.It provides a complete and easy-to-use OAuth solution. Currently, Socialite supports Facebook, Twitter, Google and Github. It looks like this:

The copy code is 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 the OAuth certification process, which can be done easily every minute. The complete documentation contains all the details about this optional package.

Flysystem integration

The new version of Laravel also includes a powerful static library for Flysystem file processing. With this library, developers can easily get started, using completely 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:

The copy code is as follows:

Storage::put ('file.txt',' contents')

For more details on Laravel Flysystem integration, you can view its complete documentation

Form request

Laravel 5.0 brings a brand new form requests, which extends from the Illuminate\ Foundation\ Http\ FormRequest class. These request objects can be combined with controller method injection to provide a new method to verify user input. Give a simple example of FormRequest:

The copy code is 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 the type hint in the controller method:

The copy code is as follows:

Public function register (RegisterRequest $request)

{

Var_dump ($request- > input ())

}

When Laravel's IoC container recognizes the type of method variable, it automatically injects an instance of FormRequest, and the request is automatically validated. This means that when your controller is called, you can safely use the input data contained in the request because they have been verified by the rules you specify in the form request class. Moreover, if the verification of the request fails, the system will automatically redirect to your predefined route and contain an error message (write to session or convert to JSON format as needed). Form validation has never been easier. For more details on FormRequest validation, please refer to the documentation.

The controller requests simple authentication

The controller base class of Laravel 5.0also includes a trait of ValidatesRequests. The trait provides a simple validate method for validating the request. If FormRequests is too heavy for your application, you can use this lightweight version:

The copy code is as follows:

Public function createPost (Request $request)

{

$this- > validate ($request, [

'title' = >' required | max:255'

'body' = >' required'

])

}

If the verification fails, an exception is thrown and the corresponding HTTP request is automatically sent to the browser. Validation errors are also written to session. If the request is initiated by AJAX, Larave automatically sends a validation error message in the form of JSON.

For more details on FormRequest validation, please refer to the documentation.

A brand new generator

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.

Configure cach

Through the config:cache command, all configuration items can be written to a cache file.

Symfony VarDumper

The auxiliary method dd, which is used to output 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:

The copy code is as follows:

Dd ([1,2,3])

Thank you for your reading, the above is the content of "what are the features of the new version of Laravel 5.0". After the study of this article, I believe you have a deeper understanding of the characteristics of the new version of Laravel 5.0. the specific use situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report