【问题标题】:Symfony Exception : value required for $request argumentSymfony 异常:$request 参数所需的值
【发布时间】:2016-04-22 17:05:58
【问题描述】:

我有一个如下所示的 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()" 要求您为 "$request" 参数提供一个值(因为没有默认值或因为在此参数之后有一个非可选参数)。

以下是路由:

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>

请帮忙...

【问题讨论】:

  • 你是否正确导入Symfony\Component\HttpFoundation\Request类?

标签: php symfony symfony-forms


【解决方案1】:

感谢 Kuba Birecki 对这个答案的评论。

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

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

【讨论】:

    【解决方案2】:

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

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      相关资源
      最近更新 更多