本文介绍了依赖项注入不适用于League\Route和League\Container的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Web应用程序,我的控制器遇到问题。

I'm building a web application right now and I'm facing a problem with my controller.

我想向我的控制器发送我的League sendPlate \引擎(已在我的容器中注册),但我仍然遇到相同的错误:传递给App\Controller\Main :: index()的参数3必须是League\Plates的一个实例\引擎,给出的数组

I want to send to my controller my League\Plate\Engine (registred in my Container) but I keep having the same error : Argument 3 passed to App\Controller\Main::index() must be an instance of League\Plates\Engine, array given

这是我的文件:

dependencies.php

use League\Container\Container;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Yajra\Pdo\Oci8;
use League\Container\ReflectionContainer;

$container = new Container();

// Active auto-wiring
$container->delegate(
    new ReflectionContainer
);

// Others dependencies
// ...

// Views
$container->add('view', function () {
    $templates = new League\Plates\Engine();

    $templates->addFolder('web', __DIR__ . '/templates/views/');
    $templates->addFolder('emails', __DIR__ . '/templates/emails/');

    // Extension
    //$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
    //$templates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));

    return $templates;
});

return $container;

routes.php

use League\Route\RouteCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$route = new RouteCollection($container);

// Page index
$route->get('/', 'App\Controller\Main::index');

// Others routes...

return $route;

Main.php

namespace App\Controller;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use League\Plates\Engine;

class Main
{
    public function index(ServerRequestInterface $request, ResponseInterface $response, Engine $templates) {
        //return $response->getBody()->write($this->templates->render('web::home'));
        return $response;
    }
}

谢谢您

编辑

我已经取得了进步。
我扩展了Main类,以扩展抽象类BaseController,如下所示:

EDIT
I've made a progress.I extended the Main class to extends the abstract class BaseController which looks like this :

namespace App\Controller;

use League\Plates\Engine;

 class BaseController
{
    protected $templates;

    public function __construct(Engine $templates) {
        $this->templates = $templates;
    }
}

第一个错误消失了,但另一个错误出现了。在Main类中,我想使用我在容器中实例化的 view 对象,但是传递给构造函数的对象是一个空的对象:

The first error goes away, but another one show up. In the Main class, I would like to use the view object that I instanciate in the container, but the object passed to the constructor is an empty one :

Main.php

class Main extends BaseController
{
    public function index(ServerRequestInterface $request, ResponseInterface $response) {
        echo '<pre>'.print_r($this->templates,1).'</pre>'; // Return an empty Plate Engine object
        return $response->getBody()->write($this->templates->render('web::home'));
        //return $response;
    }
}

这并不能解释为什么出现第一个错误

And this doesn't explain why the first error shows up

编辑2

经过一些挖掘,我终于做出了它有效,但我感觉出了点问题。
我在容器中将术语 view 替换为Engine类的命名空间:

EDIT 2
After some digging, I finally make it works, but I sense that something is wrong.I replaced in the container the term view by the namespace of the Engine class :

$container->add('League\Plates\Engine', function () {
    // The same as before
});

Main.php 中,我已经更新了索引函数,如下所示:

In Main.php I've updated the index function like this :

public function index(ServerRequestInterface $request, ResponseInterface $response) {
        $body = $response->getBody();
        $body->write($this->templates->render('web::home'));
        return $response->withBody($body);
    }

页面不会抛出500错误,并且显示html文件

And the page doesn't throw a 500 error and the html file is displayed correctly.

但是,如果我想通过 Twig 更改模板引擎怎么办?这意味着我需要将所有调用更改为 $ container-> get('League\Plate\Engine'); > $ container-> get('What\Ever'); 吗?那不是很实用!
我可能错过了一些东西!
当我要使用我的PDO对象或其他每个对象时,问题还会再次出现。

But, what if I want to change the template engine by Twig for example ? This would mean I'll need to change all the call to $container->get('League\Plate\Engine'); by $container->get('What\Ever'); ? That's not very practical!I probably missed something!And the problem will rise again when I'll want to use my PDO object... or every other object.

推荐答案

好吧,所以我通过在容器本身中注册Controllers类来解决了我的问题。

Ok so I solved my problem by registering my Controllers classes in the container itself.

例如,用于显示索引页的 Main 类调用 index 函数。在我的容器中,我呼叫

For example, for displaying the index page, the Main class call the index function. In my container, I call

$container->add('App\Controller\Main')
    ->withArgument($container->get('view'));

摘要:

bootstap.php (由 index.php 调用)

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = new \Dotenv\Dotenv(__DIR__ . '/../');
$dotenv->load();

$config = new Config(__DIR__ . '/../config/');

$container = require __DIR__ . '/../dependencies.php';

$route = require __DIR__ . '/../routes.php';

$response = $route->dispatch($container->get('request'), $container->get('response'));
$container->get('emitter')->emit($response);

dependencies.php

$container = new Container();

// activate auto-wiring
$container->delegate(
    new ReflectionContainer
);

// Others dependencies...

// Views
$container->add('view', function () {
    $templates = new League\Plates\Engine();

    $templates->addFolder('web', __DIR__ . '/templates/views/');
    $templates->addFolder('emails', __DIR__ . '/templates/emails/');

    // Extension
    //$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
    //$templates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));

    return $templates;
});

// THIS IS THE TRICK
$container->add('App\Controller\Main')
    ->withArgument($container->get('view'));
// others controllers...

return $container;

routes.php

$route = new RouteCollection($container);

// Page index
$route->get('/', 'App\Controller\Main::index');

// Others routes...

return $route;

这篇关于依赖项注入不适用于League\Route和League\Container的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:45