小编Ale*_*rov的帖子

手动验证用户

我尝试验证用户:

<?php 

/**
 * @Route("/testLogin", name="testLogin")
 */
public function testLoginAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $em->getRepository('ApplicationDefaultBundle:User')->findOneBy(array('id' => 126));

    $providerKey = 'main';
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());

    $this->container->get('security.context')->setToken($token);

    return $this->redirect($this->generateUrl('testCheck'));
}

/**
 * @Route("/testCheck", name="testCheck")
 */
public function testCheckAction()
{
    if (false === $this->get('security.context')->isGranted(
        'IS_AUTHENTICATED_REMEMBERED'
    )) {
        return new Response('Not logged');
    }
    $user = $this->container->get('security.context')->getToken()->getUser();

    return new Response($user->getUsername.' is logged');
}
Run Code Online (Sandbox Code Playgroud)

但我得到永久302重定向到/登录页面.

security:
    encoders:
        Application\Bundle\DefaultBundle\Entity\User:
            algorithm:   sha512
            iterations: 24
            encode_as_base64: true

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SPECIALIST:  ROLE_USER
        ROLE_EMPLOYER:    ROLE_USER …
Run Code Online (Sandbox Code Playgroud)

symfony

13
推荐指数
1
解决办法
2万
查看次数

在树枝模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向

我有索引操作,打印1)用于创建新实体的表单; 2)所有实体的清单:

public function indexAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $entities = $em->getRepository('MyBundle:Entity')->findAll();

    return array(
        'entities' => $entities,
    );
}
Run Code Online (Sandbox Code Playgroud)

枝条:

{% block content %}
    {% render "MyBundle:Entity:new" %}
    {% render "MyBundle:Entity:list" %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

实体控制器中的newAction是标准形式的控制器:

public function newAction()
{
    $entity = new Entity();
    $form = $this->createForm(new EntityType(), $entity);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            // NOT WORK
            return $this->redirect($this->generateUrl('entity_show',
                array('id' => $entity->getId())));
        }
    }
    return array(
        'form' => …
Run Code Online (Sandbox Code Playgroud)

php symfony

8
推荐指数
1
解决办法
3089
查看次数

标签 统计

symfony ×2

php ×1