Home  >  Article  >  Backend Development  >  About Container, Command Bus, Event in Laravel5

About Container, Command Bus, Event in Laravel5

WBOY
WBOYOriginal
2016-08-08 09:24:431688browse
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');

- 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');

- 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);

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

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());

- 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',
        ],
    ];
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
-
 <span style="white-space:pre">	</span>\Event::fire(new FirstEvent()); 
        //or use the helper function
        event(new FirstEvent());

- Queue event
plus `IlluminateContractsQueueShouldBeQueued` tag, at the same time, use
`IlluminateQueueInteractsWithQueue` trait, Use methods such as
    $this->release(30);
    $this->delete(); 
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');
        }
    
    }
How to subscribe?
    $subscriber = new App\Handlers\Events\ThirdEventHandler();
    Event::subscribe($subscriber);
    // or rely on IoC
    Event::subscribe('App\Handlers\Events\ThirdEventHandler');

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.

Statement:
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