About Container, Command Bus, Event in Laravel5

WBOY
Release: 2016-08-08 09:24:43
Original
1564 people have browsed it
About the bind method:
- These two bindings are the same. When $app->make(), both return a new instance

        $this->app->bind('App\DAO\UserDAO',function(){
        return new UserDAOImpl();
        });
        
        $this->app->bind('App\DAO\UserDAO','App\DAO\Impl\UserDAOImpl');
Copy after login

- bind can also be set alias, as follows, the first parameter is passed into the array, the key is the alias, and the value is the interface name
-
$this->app->bind(['dao.user' => 'App\DAO\UserDAO'],'App\DAO\Impl\UserDAOImpl');
Copy after login

- The third parameter is true, which means the effect of singleton is the same

$this->app->bind(['dao.user' => 'App\DAO\UserDAO'],'App\DAO\Impl\UserDAOImpl', true);
Copy after login

Command Bus
Essentially, it is a collection of calls to related methods
- Generate a Command
 php artisan make:command FirstCommand
Copy after login

At this time, a FirstCommand class will be generated under appCommands. The handle method is the specific execution content, and the handle method supports IoC.
- Call Command
Call the dispatch method in the Controller, as follows. The default Controller has `trait DispatchesCommands` so it has the dispatch method. When the following command is executed in the controller, the content in FirstCommand::handle() will be executed.
 $this->dispatch(new FirstCommand());
Copy after login

- Queue Command
Parameters will add
`IlluminateContractsQueueShouldBeQueued` interface and `SerializesModels` trait. This interface does not have any methods, just a marker (instanceof). - Pipeline command (TODO)
Event event
- Generate event class
Add the event name and the name of the Handler in the listen attribute in `EventServiceProvider`, for example:
    protected $listen = [
        'App\Events\FirstEvent' => [
            'App\Handlers\Events\FirstEventHandler',
        ],
    ];
Copy after login
Run command `php artisan event:generate`
will generate the corresponding file based on the content in $listen, and will not overwrite the already generated file.
- Processing events
Event registration is completed in the boot in `EventServiceProvider`. The handle method in Hanlder accepts a FirstEvent parameter, and handle is the processing method.
- Trigger event
-
 	\Event::fire(new FirstEvent()); 
        //or use the helper function
        event(new FirstEvent());
Copy after login

- Queue event
plus `IlluminateContractsQueueShouldBeQueued` tag, at the same time, use
`IlluminateQueueInteractsWithQueue` trait, Use methods such as
    $this->release(30);
    $this->delete(); 
Copy after login
when handling events Delete tasks, reset them, etc.
-
Subscriber
A subscriber is essentially a handler and must implement the subscribe($event) method.
    class ThirdEventHandler {
    
        /**
         * Create the event handler.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        public function doSomething(){
            echo " ThirdEventHalder !!!..";
        }
    
        public function doSomethingToo(){
            echo "lalala, ThirdEventHalder again!!!..";
        }
    
        /**
         * 注册监听器给订阅者。
         *
         * @param  Illuminate\Events\Dispatcher  $events
         * @return array
         */
        public function subscribe($events)
        {
            $events->listen('App\Events\FirstEvent', 'App\Handlers\Events\ThirdEventHandler@doSomething');
    
            $events->listen('App\Events\FirstEvent', 'App\Handlers\Events\ThirdEventHandler@doSomethingToo');
        }
    
    }
Copy after login
How to subscribe?
    $subscriber = new App\Handlers\Events\ThirdEventHandler();
    Event::subscribe($subscriber);
    // or rely on IoC
    Event::subscribe('App\Handlers\Events\ThirdEventHandler');
Copy after login

Got it.
An Event can correspond to multiple Handlers, and the execution order of Handlers is consistent with the binding order.
A Subscriber can bind multiple events
The above introduces the Container, Command Bus, and Event in Laravel5, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!