1

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

我想向我的控制器发送我的 League\Plate\Engine(在我的容器中注册),但我一直遇到同样的错误: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;

路由.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;

主文件

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,如下所示:

namespace App\Controller;

use League\Plates\Engine;

 class BaseController
{
    protected $templates;

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

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

主文件

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;
    }
}

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


编辑 2
经过一番挖掘,我终于让它工作了,但我觉得出了点问题。view我用Engine 类的命名空间替换了容器中的术语:

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

Main.php 中,我更新了 index 函数,如下所示:

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

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

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

4

2 回答 2

2

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

例如,为了显示索引页面,Main该类调用该index函数。在我的容器中,我调用

$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);

依赖项.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;

路由.php

$route = new RouteCollection($container);

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

// Others routes...

return $route;

我不知道为什么或如何工作!

于 2017-06-06T15:42:30.933 回答
1

这是有效的,因为您正在向容器注册您的控制器类,并告诉容器在构造函数中依赖注入您的 View 类..

您在此处添加的行正在执行此操作:

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

Hello World 文档示例完美地解释了它。

http://container.thephpleague.com/3.x/constructor-injection/

于 2017-11-18T04:03:01.437 回答