thephpleague / plates

Native PHP template system
https://platesphp.com
MIT License
1.47k stars 180 forks source link

Plates engine dependency injection not working #118

Closed bastienff closed 7 years ago

bastienff commented 8 years ago

Hello, i am triying to get plates to work but i am having a bit of troubles. When i implement plates without dependency injection works great. With dependency injection plates output this error:

Catchable Error: Argument 1 passed to App\Controllers\Site::__construct() must be an instance of League\Plates\Engine, none given, called in F:\xampp\htdocs\sistema\vendor\league\route\src\Route.php on line 67 and defined in F:\xampp\htdocs\sistema\app\controllers\Site.php on line 19

My code:

namespace App\Controllers;

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

class Site
{

    private $templates;
    private $templates2;

    public function __construct(\League\Plates\Engine $templates2)
    {

        $this->templates2 = $templates2;

        // No dependecy injection and working
        $this->templates = new \League\Plates\Engine();
        $this->templates->loadExtension(new \League\Plates\Extension\Asset('app/assets', true));
        $this->templates->addFolder('site', 'app/views/site');
    }

}

Win10 x64, XAMPP, PHP 5.6.21 "league/route": "^2.0", "league/plates": "^3.1.1", "zendframework/zend-diactoros": "^1.3",

baileylo commented 8 years ago

This is an issue with your dependency injection. You need to configure your DI library to automatically detect and resolve dependencies or you need to configure the Site in your DIC to inject the Engine.

AkenRoberts commented 8 years ago

league/route uses league/container under the hood. You should be able to resolve this by either manually registering the Engine class to the container, or by enabling the ReflectionContainer as a delegate (more info here).

ragboyjr commented 7 years ago

@bastienff definitely seems like this is not a Plates issue, can we close this?

ragboyjr commented 7 years ago

Closing this because it is unrelated to the Plates project itself and is a question regarding automatic dependency injection.

jasonlopes commented 3 years ago

Estou com o mesmo problema ao utilizar o plates