Symfony Exception:$ request参数所需的值

时间:2016-04-22 17:05:58

标签: php symfony symfony-forms

我的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');
        }
    }

提交表单后,我收到以下错误:

  

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

以下是路由:

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>

请帮忙......

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:3)

我认为控制器中存在问题。试试这个:

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');
          }
      }
   }

}