MichaelDaly's avatar

Command handler with array of images

I am using command handlers in my app which is a social network that allows users to upload image galleries.

I have come across an issue which I can't seem to resolve.

When my controller calls the command class I can't figure out how to pass the array of images from the input form into the command. I am declaring public properties in the command class and then trying to initiate them in the constructor.

I have been able to do this for basic inputs but just can't figure out a way to do it for arrays.

Here's an example

class CreateGalleryCommand {

 public $galleryName;

 public $galleryDesc;

 public $galleryImages[];

 public $user_id;

public function __construct($galleryName, $galleryDesc, $galleryImages[], $user_id)
   {
        $this->galleryName = $galleryName ;
        $this->galleryDesc = $galleryDesc;
        $this->galleryImages[] = $galleryImages[];
        $this->user_id = $user_id;
    }

}

Has anyone been able to do this? If so, are you able to provide advice and guidance on how to go about implementing a solution.

Thanks

0 likes
4 replies
crestheaven's avatar
public $galleryImages;
$this->galleryImages = $galleryImages;

Remove [] from argument as well. If you want to just show the property as an array do:

public $galleryImages = [];
MichaelDaly's avatar

Thanks for the quick reply. I reviewed you reply and it brought me to a mistake I was doing (apart from the obvious mistake of including [])

I was calling Input::get() which doesn't seem to pick up the image arrays from the form. So I changed this to Input::all() and everything worked as expected.

One other question I have is regarding command handlers and events. Should they be used instead of repositories or can they be used a long side them? Also, should I only use command handlers if I need to tap into events or can commands be used as a stand alone way of organising my code?

I am always trying to apply best practices but this is a really new concept to me so I am not sure if I using it correctly or if I even need it haha.

Thanks again.

crestheaven's avatar

Awesome, glad you got it working.

You can definitely use both commands and repositories. I use Laracasts/Commander along with my own implementation of repositories.

For example if you have a CreateUserCommandHandler you could use dependency injection to pass in a repository.

class CreateUserCommandHandler implements CommandHandler
{
    private $user;

    function __construct(UserRepository $user)
    {
        $this->user = $user;
    }

    public function handle($command)
    {
        $account = $this->user->create($command);

        return $user;
    }
}

UserRepository would be an interface that you bind to a specific implementation (i.e. a DbUserRepository) in a service provider.

crestheaven's avatar

And yeah - you don't necessarily even need to use events (domain). Commands are a good way of abstracting your controller logic into one location. You can find yourself repeating a lot of the same code in crud operations.

Please or to participate in this conversation.