Laravel 5 receives command response

I think I understand the concept of a command in Laravel because it is a good place to add reusable code that can be called from controllers and the like, but I have a request:

Is it possible to return the value back to the calling method from the command? For example, I have a controller method that creates a user in Active Directory that has a command to do this. If the AD server is not available, I want to return the response back to the calling controller method. Is it possible?

It only shows in the documentation how to invoke a command using dispatch()

, but has nothing to say about whether it can return anything.

And if you can't return a value, can someone explain the reasons why you don't want to return a value? I know that the commands in the queue can take a while and won't wait for a response, but for commands that need to be executed immediately, I don't understand why you wouldn't want to return a value.

Any help or advice is greatly appreciated.

+3


source to share


2 answers


In the context of a command bus, yes, you can return values ​​for out-of-order commands. In the command handler method, just return what you want:

public function handle(){
    return 'foobar';
}

      



And save the result of the send command to a variable:

 $my_command_result = $this->dispatch(
    new MyCommand();
);

      

+6


source


Commands are not meant to store reusable code in controllers. Theyre being renamed to jobs in 5.1 and their main purpose is to work in Cron Jobs. If you have a way to create a user and want to use it in many places, you can store it in the user model.



+2


source







All Articles