Symfony异常:$ request参数所需的值

sam*_*nek 3 php symfony-forms symfony

我有一个loginAction看起来像这样:

public function loginAction(Request $request){
    if($request->getMethod() == 'POST'){
        $mail = $request->getContent('umail');
        $pass = $request->getContent('upass');
        $em = $this->getDoctrine()->getManager();
        $rep = $em->getRepository('SystemBundle:User');

        $user = $rep->findOneBy(array("email"=>$mail,"pass"=>$pass));
        if($user){
            $id = $user->getId();
            $type = $user->getType();

            return $this->render('@System/Pages/admin/index.html.twig');
        }
    }
Run Code Online (Sandbox Code Playgroud)

提交表格后,我得到以下错误:

控制器“ SystemBundle \ Controller \ SystemController :: loginAction()”要求您为“ $ request”参数提供一个值(因为没有默认值,或者因为此值之后没有非可选参数)。

以下是路由:

system_login:
path: /login
defaults: { _controller: SystemBundle:System:login}
Run Code Online (Sandbox Code Playgroud)

以及形式:

<form method="POST" id="lgn" action="{{ path('system_login') }}">
        <span class="fa fa-times close" onclick="lgn()" ></span><br />
        <span>Login:</span>
        <input type="email" placeholder="Email" required="required" name="umail" />
        <input type="password" placeholder="Password" required="required" name="upass" />
        <button type="submit">login</button>
    </form>
Run Code Online (Sandbox Code Playgroud)

请帮忙...

小智 5

感谢Kuba Birecki对此答案的评论。

Symfony无法自动装配$ request参数,因为它与可用的可自动装配参数类型列表不匹配。这可能是因为,当您添加Request typehint时,您的IDE use为错误的类添加了一条语句。

确保该use声明适用于Symfony\Component\HttpFoundation\Request