I'm running into some trouble while using the IoC container in Laravel 4.2
I have 2 classes: BlockFactory
and Block
. They are meant to display blocks of content (much like joomla modules) where a single Block
represents a block of content and the BlockFactory
represents the "container" of those blocks. This BlockFactory
has a render method I want to call to render out all the blocks.
What I did was create a BlockServiceProvider like so:
public function register()
{
$this->app->singleton('BlockFactory', function () {
return new BlockFactory();
});
$this->app->bind('block', 'Animekyun\Block\Block'); // Facade
}
I'm injecting the BlockFactory
class into the Block
constructor so I can add blocks to the factory:
public function __construct(BlockFactory $blockFactory)
{
$this->blockFactory = $blockFactory;
}
public function create($title, $content, $parameters)
{
$this->title = $title;
$this->content = $content;
$this->parameters = $parameters;
return $this;
}
public function push()
{
$this->blockFactory->add($this);
}
From what I understand is that when I use the singleton method with the IoC container I can access the BlockFactory
from within the container and it will be the same instance over and over again. Something like a global variable.
However I have not been able to access BlockFactory
in anyway without instantiating it again. I tried App::make('BlockFactory')
but according to the documentation it just calls the callback defined in the service provider.
Is this possible? What are some other ways of doing this?