Sometimes when we simply look at the Laravel manual there will be some doubts, such as the authorization and events under the system service, what are the application scenarios of these functional services, in fact, if you have not experienced a certain development experience have these doubts is very normal things, But when we think more about our work, we can see that sometimes we've seen these services in fact. Here's a very simple example of event and event monitoring, and you'll find out. This example is about the implementation of the article browsing number, when the user view the article when the number of articles will be increased by 1, the user to view the article is an event, with the event, you need an event listener, to listen to the event after the execution of the corresponding operation (article views plus 1), in fact, this monitoring mechanism in Laravel is implemented through the observer Pattern . registering events and listener apps Eventserviceprovider in the /providers/ directory . Register the Event listener mapping relationship in PHP as follows:
protected $listen = [ ' app\events\blogview ' = [' App\listeners\blogviewlistener ', ], ];
then execute the following command under the project root directory PHP artisan event : Generate The command completes,blogview.php and Blogviewlistener are automatically generated in the app/events and App/listensers directories, respectively. php files. Defining Events
<?phpnamespace app\events; Useapp\events\event; UseApp\post; UseIlluminate\queue\serializesmodels; UseIlluminate\contracts\broadcasting\shouldbroadcast;classBlogviewextendsevent{ UseSerializesmodels; /** * Create a new event instance. * * @return void*/ Public function__construct (Post$post) { $this->post =$post; } /** Get The channels the event should is broadcast on. * * @return Array*/ Public functionBroadcaston () {return []; }}
actually seeing these you will find that the event class just injects a post instance, and does not contain extraneous logic. Define listener event listeners receive event instances in the handle method,event: Thegenerate command automatically imports the appropriate event class and type hint events in the handle method. Within the handle method, you can execute any required logic in response to the event, and our code is implemented as follows:
<?phpnamespace app\listeners; UseApp\events\blogview; UseIlluminate\queue\interactswithqueue; UseIlluminate\contracts\queue\shouldqueue; UseIlluminate\session\store;classblogviewlistener{protected $session; /** * Create the event listener. * * @return void*/ Public function__construct (Store$session) { $this->session =$session; } /** * Handle the event. * * @param blogview $event * @return void*/ Public functionHandle (Blogview$event) { $post=$event-Post; //first make a decision whether you have viewed if(!$this->hasviewedblog ($post)) { //Save to database $post->view_cache =$post->view_cache + 1; $post-Save (); //saved to Session after you've seen it $this->storeviewedblog ($post); } } protected functionHasviewedblog ($post) { return array_key_exists($post->id,$this-getviewedblogs ()); } protected functiongetviewedblogs () {return $this->session->get (' Viewed_blogs ', []); } protected functionStoreviewedblog ($post) { $key= ' viewed_blogs. '$post-ID; $this->session->put ($key, Time()); }}
Some logic has also been explained in the comments. After triggering event and event monitoring, all we have to do is implement the entire listener, the fire method that triggers the user to open the article event, which we use and event provides, as follows:
<?phpnamespace app\http\controllers; Useilluminate\http\request; UseApp\post; Useilluminate\support\facades\event; Useapp\http\requests; UseApp\events\blogview; UseApp\http\controllers\controller;classBlogControllerextendscontroller{ Public functionShowpost ($slug) { $post= Post::whereslug ($slug),Firstorfail (); Event:: Fire (NewBlogview ($post)); returnView (' Home.blog.content ')->withpost ($post); }}
Now open the page found in the database of the ' View_cache has been normal add 1, so the whole is done.
Event events action in the Laravel framework