Aura.Di as a Singleton?

45 views
Skip to first unread message

Ariel Lu

unread,
Jan 24, 2015, 4:24:05 PM1/24/15
to aur...@googlegroups.com
Hello! I've just started using Aura2 to play around on the weekends, there's things I like and MANY I don't understand from this framework, so I thought I could bother you with one question from the top of my head.

I'm reading about dispatching as a microframework, so I need this functions to start a route and a dispatcher :

public function define(Container $di)
public function modify(Container $di)

and on my action:

public function __construct(Request $request, Response $response)

which receives


$di
->get('aura/web-kernel:request');
$di
->get('aura/web-kernel:response');

So it seems to me I'm passing this variable $di around all the time, and my question is why? why is not just a global variable, a singleton, or something I can just
access without writing code over and over.

If my Action also needs Doctrine for my database I would have to pass something like $di->get("doctrine") on every action?

While I'm at it, is there an example project from Aura2 to study? I skimmed through github and google but haven't seen one.

Thanks and greetings from Argentina.


Brandon Savage

unread,
Jan 24, 2015, 4:59:55 PM1/24/15
to aur...@googlegroups.com
The DI container isn't meant to be passed into an object as a dependency; it's meant to be used for creating other objects. That means you shouldn't be accessing it inside objects, except in very limited circumstances (e.g. a front controller that needs to create a controller). Making it a singleton wouldn't achieve these goals.

Brandon

--
You received this message because you are subscribed to the Google Groups "The Aura Project for PHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to auraphp+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hari K T

unread,
Jan 24, 2015, 8:42:44 PM1/24/15
to aur...@googlegroups.com
Hey, 

Welcome to aura.

What you can do is  extend an AbstractAction .

abstract class AbstractAction
{
    public function __construct(
        Request $request,
        Response $response
    ) {
        $this->request = $request;
        $this->response = $response;
    }
}

Now all your class will be something like 

class SomeAction extends AbstractAction {}

The define method is where you define the di stuffs to be passed.

    $di->params['Namespace\AbstractAction']['response'] = $di->lazyGet('aura/web-kernel:response');
    $di->params['Namespace\AbstractAction']['request'] = $di->lazyGet('aura/web-kernel:request');

Now in your dispatcher use the $di->setObject('someclass', $di->lazyNew('SomeClass'));

You can see more examples on how I configured over 


If you are interested to know how the define and modify works, have a look at the post "Composer-Assisted Two-Stage Configuration" :  http://auraphp.com/blog/2014/04/07/two-stage-config/

I hope these links will help you to some extend. If not please ask. Happy to help.

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt

Paul M. Jones

unread,
Jan 25, 2015, 2:15:19 AM1/25/15
to aur...@googlegroups.com

> On Jan 24, 2015, at 15:24, Ariel Lu <ariellur...@gmail.com> wrote:
>
> Hello! I've just started using Aura2 to play around on the weekends, there's things I like and MANY I don't understand from this framework, so I thought I could bother you with one question from the top of my head.
>
> I'm reading about dispatching as a microframework, so I need this functions to start a route and a dispatcher :
>
> public function define(Container $di)
> public function modify(Container $di)
>
> and on my action:
>
> public function __construct(Request $request, Response $response)
>
> which receives
>
>
> $di->get('aura/web-kernel:request');
> $di->get('aura/web-kernel:response');
>
> So it seems to me I'm passing this variable $di around all the time, and my question is why?


If you have not done so already, you should read the documentation on the dependency injection package: <https://github.com/auraphp/Aura.Di>

Using a DI container can be a hard subject to grasp, but in short, you don't pass the $di instance around. Instead, all of the object creation happens *inside* the $di instance.

Regarding your action, you say you define your class like this ...

class MyAction
{
public function __construct(Request $request, Response $response)
{
// ...
}
}

... which is just fine, but the part you need now is to set up the the container configuration (this is the config/Common.php file at the top level of the project):

class Common
{
public function define(Container $di)
{
$di->params['MyAction']['request'] = $di->get('aura/web-kernel:request');
$di->params['MyAction']['response'] = $di->get('aura/web-kernel:response');
}
}


Let me know if that begins to help.



--
Paul M. Jones
pmjo...@gmail.com
http://paul-m-jones.com

Modernizing Legacy Applications in PHP
http://mlaphp.com



Ariel Lu

unread,
Jan 25, 2015, 9:18:11 AM1/25/15
to aur...@googlegroups.com

Aura.Blog it's just what I needed to see how the framework works, and Im gonna read more from the Aura.Di component page and dependency injection in general.

Thank you all for your responses.

Reply all
Reply to author
Forward
0 new messages