2017-06-03 60 views
7

这是我的控制器简单的登录表单使用symfony的

use Symfony\Component\HttpFoundation\Request; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; 

class SecurityController extends Controller 
{ 
/** 
* @Route("/login", name="login") 
*/ 
public function loginAction(Request $request,AuthenticationUtils $authUtils) 
{ 
    // get the login error if there is one 
    $error = $authUtils->getLastAuthenticationError(); 

    // last username entered by the user 
    $lastUsername = $authUtils->getLastUsername(); 

    return $this->render('blog/login.html.twig', array(
      'last_username' => $lastUsername, 
      'error'   => $error, 
    )); 
} 
} 

这是我security.yml

providers: 
    our_db_provider: 
     entity: 
      class: AppBundle:user 
      property: uname 

    in_memory: 
     memory: 
      users: 
       clement: 
        password: $2y$12$Z2B4JTnglzaYs4z73DBh9u/hIDN/E56CCrLKIjQxP6Q7aeLb5S6LO 
        roles: 'ROLE_ADMIN' 
       admin: 
        password: symfony 
        roles: 'ROLES_ADMIN' 
       ryan: 
        password: ryan1234 
        roles: 'ROLES_USER' 


encoders: 
    Symfony\Component\Security\Core\User\User: 
      algorithm: bcrypt 
      cost: 12 
    AppBundle\Entity\User: 
      algorithm: bcrypt  



firewalls: 
    # disables authentication for assets and the profiler, adapt it according to your needs 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 


    main: 
     anonymous: ~ 
     # activate different ways to authenticate 

     # http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate 
     http_basic: ~ 

     # http://symfony.com/doc/current/cookbook/security/form_login_setup.html 
     form_login: 
      login_path: login 
      check_path: login 


    secured_area: 
     pattern: ^/ 
     provider: our_db_provider 
     anonymous: ~ 
     logout: true 

access_control: 
    # require ROLE_ADMIN for /admin* 
    - { path: ^/admin, roles: ROLE_ADMIN } 

这是我的看法

<form action="{{ path('login') }}" method="post"> 
<label for="username">Username:</label> 
<input type="text" id="username" name="_username" value="{{ last_username }}" /> 

<label for="password">Password:</label> 
<input type="password" id="password" name="_password" /> 

{# 
    If you want to control the URL the user 
    is redirected to on success (more details below) 
    <input type="hidden" name="_target_path" value="/account" /> 
#} 

<button type="submit">login</button> 
</form> 

而且我得到这个错误。

控制器“AppBundle \ Controller \ SecurityController :: loginAction()”要求您提供“$ authUtils”参数的值。参数是可为空的,并且没有提供空值,没有提供默认值,或者因为在这之后有一个非可选参数。

+0

您正在使用多个防火墙,您确定使用正确的登录表单吗? 'secured_area'例如没有login_form,并且匹配'^ /'。 – ccKep

+0

我现在改成了main,而且仍然一样 –

+3

错误消息来自$ authUtils没有设置的事实。将authUtils作为动作参数传递给S3.3是新的。我怀疑你使用的是早期版本?按照3.2的例子:https://symfony.com/doc/3.2/security/form_login_setup.html是的,你的防火墙看起来不寻常。 – Cerad

回答

7

这是不正确的回答你的问题,但一个可能的修复

  1. 改变方法签名public function loginAction(Request $request)
  2. 在你的行动
2

的开始。如果在Symfony的添加$authUtils = $this->get('security.authentication_utils'); 3.3此功能不起作用,您需要:

  1. 启用自动装配
  2. 标签的控制器,controller.service_arguments

但是,这是在默认设置symfony的,所以你只需要你清除/更换default services.yml去改变它。

如果你不想使用autowire,你可以明确地define argument types

+0

“SymfonyKillers”的朋友告诉我这个..我不明白,因为我正在开发Symfony 3.1。 我刚刚升级... – Delphine

+0

我有这个问题,我正在使用Symfony 3.3.0。 Autowire已启用且控制器已被标记。有点奇怪,因为我认为这适用于所有3.3.x +版本。奇怪的是,在我使用3.3.4的另一个项目中,使用相同的代码,它确实按预期工作。 –