akrabat / slim3-skeleton

Simple Slim Framework 3 skeleton with Twig & Monolog
BSD 3-Clause "New" or "Revised" License
345 stars 99 forks source link

new Action Class DI error #28

Closed JefferyWong closed 8 years ago

JefferyWong commented 8 years ago

I create a new Class in app/src/Action which namespace is App\Action too. And use exactly the same constructor with HomeAction . I use it in route file but got next error: Catchable fatal error: Argument 1 passed to App\Action\ProductAction::__construct() must be an instance of Slim\Views\Twig, instance of Slim\Container given, called in /Users/tinker010/zhengjie/kafe/vendor/slim/slim/Slim/CallableResolver.php on line 64 and defined in /Users/tinker010/zhengjie/kafe/app/src/Action/ProductAction.php on line 21

Don't know why, here is my New Class: <?php namespace App\Action;

use Slim\Views\Twig; use Psr\Log\LoggerInterface; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use Httpful\Request as Rest; use Httpful\Mime; use Eventviva\ImageResize;

final class ProductAction { private $view; private $logger;

public function __construct(Twig $view, LoggerInterface $logger)
{
    $this->view = $view;
    $this->logger = $logger;
}

public function productAddPage(Request $request, Response $response, $args)
{
    return $this->view->render($response, 'product.html');
}

}

JefferyWong commented 8 years ago

I checked https://akrabat.com/a-slim3-skeleton/ again Do I have to add every Action class file to dependencies.php like HomeAction?

Zyles commented 8 years ago

I have the same problem.

Fixed with adding it to dependencies.php

Antnee commented 8 years ago

@JefferyWong The default is to pass the container to the action. That's not ideal as you're passing in a generic DI container and not declaring your dependencies. So no, you don't HAVE to add every action in dependencies.php, but it's better that you do, and declare your action's dependencies in the constructor