Laravel WEB APP based on container event container events

Source: Internet
Author: User
Description: This article focuses on Laravel container events, and makes a simple demo of container events to deepen understanding of container events. At the same time, the author will be in the development process of some and code to stick up, improve reading efficiency.

Container is the core of the Laravel framework, with a variety of service stores in container, and each service is registered in container via service provider. The facade mode helps us to parse the required service objects from the container. In this process, each time the container parses an object from the container, it triggers an event that can be heard through the resolving method. In fact, in the Laravel framework, the form request validation using this good tool, through a form request class to implement the form content verification, so as not to put logic in the controller control, in particular, can see Chinese documents: Form request validation. About Container event You can see the document: Container events.

Application of Container event in form request

To write a route first:

Route::p ost (' ContainerEvent ', ' containereventcontroller@containerevent '); Route::p ost (' Formrequest ', ' containereventcontroller@formrequest '); Route::get (' container ', ' containereventcontroller@profile ');

Build a controller again:

PHP Artisan Make:controller Containereventcontroller

Write the method:

    Public Function containerevent ()    {                } public Function    formrequest ()    {    } public    function Profile ()    {        return view (' profile ');    }

Write the View:

            
 
          
 
          
 
          
 
          Bootstrap Template        
 
          
 
                                                                                                                      
 
                  
 
                      

Write a form Request class: Enter the command first to generate the form request class

PHP Artisan make:request containerformrequest

and give the validation rules.

Class Containerformrequest extends request{    /**     * Determine if the user is authorized to make this Request.     *     * @return BOOL     *    /Public Function authorize ()    {        return true;//to True, this is generally used as user authentication, all through validation    }    /**     * Get The validation rules that apply to the request.     *     * @return Array     *    /Public Function rules ()    {        return [            ' name ' = ' Required ',            ' Age '  = ' required|numeric|min:18 ',        ];    }}

Modify Containereventcontroller:

Public Function Formrequest (containerformrequest $containerFormRequest) {        dd (' This is a from Request Container Event. IT is working!!! ');}

At the same time, the app/http/kernel.php file \app\http\middleware\verifycsrftoken::class logoff, or submit the form Tokenmismatchexception error.

OK, input route (modified to your route): Http://laravelcontainerevent.app:8888/container, the input error form will return to the current form page and will be printed when the input form is submitted correctly:

Note that Fromrequest is already working, containerformrequest this object will work authorize and the rules method when parsing from the container. and the controller only need to inject containerformrequest this object is OK.

Demo

Implement a custom class that implements the same functionality as the form submission. To create a new eventbeforeresolving.php file in the App/contracts folder:

namespace App\contracts;interface eventbeforeresolving{public    function Isadult ();

And under a service provider registration:

appserviceprovider.php/**     * Register any application services.     *     * @return void     *    /Public Function register ()    {//When the solution is injected into the controller before The Isadult () method that implements the Eventbeforeresolving interface object is called First        $this->app->resolving (Eventbeforeresolving::class, function ($obj, $app) {            $obj->isadult ();        });    }

Write a service to implement this interface:

App/services/authorize.phpnamespace App\services;use App\contracts\eventbeforeresolving;use Illuminate\Http\ Request;class authorize implements eventbeforeresolving{    private $request;    Public function __construct (Request $request)    {        $this->request = $request;    }    Public Function Isadult ()    {        $name = $this->request->input (' name ');        $age = $this->request->input (' age ');        if (Empty ($name) | | | empty ($age) | | ($age < 18)) {            dd (' Name ' and age must is required. And age must is above ");}}}    

Modify the following containereventcontroller:

    Public Function ContainerEvent (authorize $authorize)    {        dd (' This is a Demo Container Event. IT is working!!! ');    }

Also do not forget to modify the form submitted in the next profile.blade.php file action= '/containerevent '.

An error message will be prompted when an error is entered:

The Container event is to trigger events before the service object resolves the injection from the container, which can be used to do interesting and useful things, such as the form request validation of the Laravel framework, which does not put the validation logic code in the controller. Avoid messing with the controller.

Summary: This section mainly describes Laravel container events, and takes form Requet as an example to illustrate its purpose, and a small demo of how to build and listen to the container events step-by-step. Well, after two days also want to combine Laravel task schedual tasks to schedule a new chapter, to the meeting.

  • Related Article

    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.