I'm trying to create a little block presenter for Laravel which displays blocks of content in certain positions. I have 2 classes, BlockFactory and Block. The BlockFactory contains all the blocks and creates them whereas the Block class is exactly just that: a simple block.

As can be seen in the code below, when I'm creating a new Block from within the BlockFactory, I have to pass all these parameters to the create method. Which then get passed onto the instantiation of the Block class and finally passed onto the constructor.

This seems very unwieldy to me. Any ideas for a smarter way of doing this?

Also: not sure if it's smart to create a new Block object inside create method. I was trying to think in terms of dependency injection but I couldn't figure it out.

// create and save new block
Block::create('name', 'content', 'position', 0, [])->add();

// BlockFactory, bound to the Block facade (bad naming i know)
class BlockFactory

    protected $blocks = [];
    protected $currentBlock;

    public function create($title, $content, $position, $order = 0, $parameters = [])
        $this->currentBlock = new Block($title, $content, $position, $order, $parameters); // this part is bugging me somehow...
        return $this;

    public function add()
        $this->blocks[] = $this->currentBlock;

    public function render()
        return $this->blocks;


// The actual block class
class Block

    protected $title;
    protected $content;
    protected $position;
    protected $order;
    protected $parameters;

    public function __construct($name, $content, $position, $order = 0, $parameters = [])
        $this->title = $name;
        $this->content = $content;
        $this->position = $position;
        $this->order = $order;
        $this->parameters = $parameters;

1 Answer 1


The problem might be the semantic reference you are using as a pattern. You are creating a class as a "factory", but the actual Factory patter is related only to creating objects, and it seems that what you mean is actually the "Repository" pattern, where you can created, manage and retrieve objects.

And your case you would create a BlockRepository that it would be injected in the controller to be the interface between the controller and the actual blocks you need to create and retieve.

You can find related references on the links below:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.