猿问

为什么 silex 不能在构造函数中解析 A​​pp?

当我尝试在构造函数中使用自动解析依赖项时,我收到一个错误,尽管在方法中它工作正常。



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


白板的微信
浏览 95回答 1
1回答

子衿沉夜

$app定义路由时必须作为参数传递给构造函数$app->post(&nbsp; &nbsp; '/author',&nbsp; &nbsp; function (Request $request) use ($app) {&nbsp; &nbsp; &nbsp; &nbsp; $controller = new AuthorController(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $app,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $request&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; return $controller->create();&nbsp; &nbsp; });
随时随地看视频慕课网APP
我要回答