繁体   English   中英

使用symfony的简单登录表单

[英]simple login form using 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”参数提供值。 参数可以为空,并且没有提供空值,没有提供默认值,或者因为在此之后存在非可选参数。

这不是您问题的正确答案,而是可能的解决方案

  1. 将方法签名更改为public function loginAction(Request $request)
  2. add $authUtils = $this->get('security.authentication_utils'); 在你的行动开始时

如果在Symfony 3.3中此功能不起作用,您需要:

  1. 启用自动装配
  2. 使用controller.service_arguments标记您的controller.service_arguments

这是symfony中的默认设置,因此如果清除/替换默认services.yml ,则只需更改它。

如果您不想使用autowire,则可以显式定义参数类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM