我需要在类内部定义一个用于依赖项注入的容器吗?

问题描述 投票:0回答:1

我正在学习Auraphp Di,我想编写示例代码。假设我有这些文件:

public / index.php:

use Aura\Di\ContainerBuilder;
use MyPackage\Component\Authentication\AuthenticateFlow;

require_once dirname(__DIR__) . '/vendor/autoload.php';

$builder = new ContainerBuilder();
$di = $builder->newInstance();

$di->set('authenticateFlow', $di->lazyNew(AuthenticateFlow::class));

$authenticateFlow = $di->get('authenticateFlow');

$authenticateFlow->showName('Belkin');

/ src / Components / Authentication / AuthenticationFlow.php:

namespace MyPackage\Components\Authentication;

class AuthenticationFlow
{
    public function showName($name)
    {
        echo $name;
    }
}

这很好。现在假设我有另一个类(/src/Components/Authentication/Filter.php),它具有一个名为filterInput:

的方法。
namespace MyPackage\Components\Authentication;

class Filter
{
    public function filterInput($input)
    {
        return htmlspecialchars($input);
    }
}

我如何将过滤器注入AuthenticationFlow,以使用filterInput()方法?我想在AuthenticationFlow :: showName():

中添加类似的内容
echo $this->filter->filterInput($name);

我知道我需要在AuthenticationFlow构造函数中注入Filter类,但是我不知道是否可以使用index.php中内置的容器。如果我需要在AuthenticationFlow中创建另一个容器,index.php将如何知道它?

php dependency-injection auraphp
1个回答
0
投票

您的应用程序需要大量使用di容器,以便注入必要的依赖关系。这不是光环的情况。

让我们退后一步,看看如果不使用容器该怎么办。

为了在Filter中使用AuthenticationFlow对象,您需要通过构造函数或setter方法注入Filter对象。在下面的示例中,我正在使用构造函数注入。

class AuthenticationFlow
{
    protected $filter;

    public function __construct(Filter $filter)
    {
        $this->filter = $filter;
    }

    public function showName($name)
    {
        return $this->filter->filterInput($name);
    }
}

所以您将如下创建AuthenticationFlow的对象。

$auth = new AuthenticationFlow(new Filter);

[对于Aura.Di,您可以做类似的事情

$object = $di->newInstance(AuthenticateFlow::class);

如果关闭了自动解析,则需要如下定义依赖项

$di->params[AuthenticateFlow::class]['filter'] = $di->lazyNew(Filter::class);

在应用程序中,这不是正确的。您可能需要将AuthenticateFlow放在另一个HelloController::class上。

Class HelloController
{
    protected $auth;

    public function __construct(AuthenticationFlow $auth)
    {
        $this->auth = $auth;
    }

    public function execute()
    {
        // Do something 
    }
}

因此,在这种情况下,HelloController::class需要通过di本身实例化。否则,依赖项将不会自动注入。

$object = $di->newInstance(HelloController::class);

您可以将Aura\Di\ContainerConfigdefine services扩展为多个类别。

示例:

namespace YourVendor;

use Aura\Di\Container;
use Aura\Di\ContainerConfig;
class Config extends ContainerConfig
{
    public function define(Container $di)
    {
        $di->set(HelloController::class, $di->lazyNew(HelloController::class));
        $di->params[HelloController::class]['auth'] = $di->lazyNew(AuthenticateFlow::class);
        $di->params[AuthenticateFlow::class]['filter'] = $di->lazyNew(Filter::class);
    }

    public function modify(Container $di)
    {
        // You can get the service and modify if needed
        // $auth = $di->get('authenticateFlow');
    }
}

现在您的index.php将看起来像,

require_once dirname(__DIR__) . '/vendor/autoload.php';

$builder = new ContainerBuilder();
$di = $container_builder->newConfiguredInstance([
    'YourVendor\Config',    
]);

$hello = $di->newInstance(HelloController::class);
$hello->execute();

正如我在前面的答案中提到的,我建议您先阅读文档。从长远来看,它将真正帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.