5 years ago

Command Bus Stuff in Laravel 4.3

Posted 5 years ago by JeffreyWay

One thing Laravel 4.3 introduces is a new system for validating and authorizing form requests.

Now, if you think about it, form requests can function as commands. Yes, they're more than dumb, immutable DTOs, however they still carry the data we require (and can easily be cast to StdClasses), as well as a name that perfectly describes the instruction from the user.

So, that means, if you wanted to, you could very easily implement your own makeshift command bus type setup. You don't need a package for this. For instance:

<?php namespace Laracasts\Http\Controllers;

use Illuminate\Routing\Controller;
use Laracasts\Core\ExecutableTrait;
use Laracasts\Http\Requests\ScheduleTaskRequest;

class TasksController extends Controller {

    use ExecutableTrait;

     * Schedule a new task.
     * @param ScheduleTaskRequest $request
     * @return Response
    public function store(ScheduleTaskRequest $request)
        // At this point, validation and authorization have passed.

        // So let's handle this instruction.
        // This will trigger Laracasts\Handlers\ScheduleTaskHandler

        return Redirect::somewhere();


And then your execute method (or dependency) could just determine which handler to resolve out of the container, and then call it, while passing through something like (object) $request->all().

<?php namespace Laracasts\Handlers;

class ScheduleTaskHandler implements CommandHandler {

     * @param $task
    public function handle($task)
        // delegate to schedule the task


With this approach, you get away from that issue, where your naming convention for form requests and commands are incredibly similar.

Has anyone experimented with this approach yet? (Or need more info?) Any downsides? Just thinking out loud right now.

Please sign in or create an account to participate in this conversation.