为什么Silex无法在Consructor中解析App?

时间:2019-07-30 14:21:23

标签: php symfony silex

当我尝试在构造函数中使用自动解析依赖项时,出现错误,尽管在方法上可以正常工作。


<?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

1 个答案:

答案 0 :(得分:1)

定义路由时,必须将$app作为参数传递给构造函数

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