Custom Ajax Request Response classes in laravel5.4

Source: Internet
Author: User

I small white in the IT industry for a year long time no results just so to share with you, in the process of work and learning has been the pursuit of beauty, but for me, after all, limited strength, whenever encountered some return or the site or the project back to the user's friendly hints always feel headache, Direct alert It really is too low so combine the laravel5.4 used to share to everyone a custom cue class

First, let's take a look at the official documentation.

The document has been explicitly expressed in the Config folder contains all the configuration files, then our configuration file can be placed in the configuration file, so name a file as constants.php file You can customize what I'm going to do.

return [

/**
* Ajax Status Description
* 1. Success (?? ); 0. Warning (?? ); 2. Error (?), 3. Question (?) 4. Permissions (?? )
*/
' Status_success ' = 1,
' Status_danger ' = 0,
' Status_error ' = 2,
' Status_doubt ' = 3,
' Status_refuse ' = 4,


/**
* Http Status Code
*
*/
' Http_status_bad ' = ' 400 ',//Error request
' Http_status_timeout ' = ' 408 ',//Request timed out
' Http_status_conflict ' = ' 409 ',//Conflict status code
' Http_status_grammar ' = ' 400 ',//syntax error, server not recognized
' Http_status_no_accept ' = ' 406 ',//server not Accepted
' Http_status_server_error ' = ' 500 ',//server Internal Error

So the question is , how do we read our configuration under the Config folder?

You can use the Config () method in the Laravel framework at this time (let's call him method) so the specific use is as follows

\config::get (' constants.status_success '), the Config method is obtained by getting, and then it is the file name. Configuration item name but that's where we put the hint class for friendly information , I create a folder under the app tools to create a file from underneath header.php with this as a hint of friendly information class of course create don't be too.

In this way, we have just set up the configuration item loaded into the, if you feel that you need to make some text hints then you can add your own language after Resources\lang folder add your own words can then config/app.php the following changes to their language pack

So I'm using a custom language pack myself, and in the case of a friendly hint, you create the ZH-CN folder in Resources\lang, and then you can define the same array form yourself in the common conmmon.php specific hints language.

' request_successful ' = ' request success ',
' Request_field ' = ' request failed ',
' Parameter error ' = ' parameter wrong ',
' Nonexistence_user ' = ' non-existent user ',

The icons and text messages that combine our friendly tips are used as follows

function Ajax_succeed ()
{
return [' Status ' =>\config::get (' constants.status_success '), ' message ' =>trans (' common.request_successful ')] ;
}

So when we return to the front desk in the controller, we directly

return \app\tools\ajax_succeed ();

Then the reception after receiving the use of the layer plug-in this we can find their own

Data is the return value of the callback function, so that our cue message is defined by itself.

Custom Ajax Request Response classes in laravel5.4

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.