ogbofjnr
ogbofjnr

Reputation: 2008

Why silex don't resolve App in consructor?

When I'm trying to use auto resolving dependencies in constructor I get an error, although in method it works ok.


<?php

namespace App\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

class AuthorController
{

// Produce an error
//    public $app;
//    public $request;
//    public $entityManager;
//
//    public function __construct(Application $app, Request $request)
//    {
//        $this->app=$app;
//        $this->request=$request;
//    }

    public function create(Application $app, Request $request)
    {

    }
}

Argument 1 passed to App\Controller\AuthorController::__construct() must be an instance of Silex\Application, none given

Upvotes: 0

Views: 51

Answers (1)

Caconde
Caconde

Reputation: 4483

You must pass $app as a parameter to the constructor when defining the route

$app->post(
    '/author',
    function (Request $request) use ($app) {
        $controller = new AuthorController(
            $app,
            $request
        );
        return $controller->create();
    }
);

Upvotes: 1

Related Questions