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

Symfony Exception : value required for $request argument

我有一个看起来像这样的 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');
        }
    }

提交表单后出现以下错误:

Controller "SystemBundle\Controller\SystemController::loginAction()" requires that you provide a value for the "$request" argument (because there is no default value or because there is a non optional argument after this one).

路由如下:

system_login:
path: /login
defaults: { _controller: SystemBundle:System:login}

和表格:

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

请帮忙...

我认为你的控制器有问题。试试这个:

use Symfony\Component\HttpFoundation\Request;

class FooController extends Controller
{

    public function loginAction(Request $request)
    {

       if($request->getMethod() == 'POST')
       {
          $mail = $request->request->get('umail');
          $pass = $request->request->get('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');
          }
      }
   }

}

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

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

确保 use 语句适用于 Symfony\Component\HttpFoundation\Request