dongtu9823 2015-12-16 12:36 采纳率: 0%
浏览 96
已采纳

在Symfony 3 Controller中获取请求和会话

I have a big problem and please help me. So I want to create an login system witch will use only users from :

providers:
  in_memory:
      memory:
          users:

So my global routing : /app/config/routing.yml :

app_admin:
    resource: "@AppAdminBundle/Resources/config/routing.yml"
    prefix:   /admin

My routing in AdminBundle :

app_admin_homepage:
    path:     /
    defaults: { _controller: AppAdminBundle:Login:index }
login:
    path:   /login
    defaults:  { _controller: AppAdminBundle:Login:login }
login_check:
    path:   /login_check

My LoginController :

<?php

namespace App\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

class LoginController extends Controller
{
public function indexAction()
{
    return $this->render('AppAdminBundle:Member:login.html.twig');
}
public function loginAction()
{
    $request = $this->get('request');
    $session = $this->get('session');

    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render('AppAdminBundle:Member:login.html.twig', array(
        'last_username' => $session->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    ));
}
}

My login.html.twig :

<form class="login-form" action="{{ path('login_check') }}">
    <div class="login-wrap">
        <p class="login-img"><i class="icon_lock_alt"></i></p>
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_profile"></i></span>
            <input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" placeholder="Username" autofocus>
        </div>
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_key_alt"></i></span>
            <input type="password" class="form-control" id="password" name="_password" placeholder="Password">
        </div>
        <button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
    </div>
</form>

And my security.yml :

security:
role_hierarchy:
  ROLE_ADMIN:       ROLE_USER
  ROLE_FMTI:        ROLE_FMTI
  ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
      - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/admin, role: IS_AUTHENTICATED_ANONYMOUSLY }

firewalls:
  dev:
      pattern:  ^/(_(profiler|wdt)|css|images|js)/
      security: false

  secured_area:
      pattern:    ^/admin
      anonymous: ~
      form_login:
          login_path:  /admin/login
          check_path:  /admin/login_check


 access_control:
  - { path: ^/admin, roles: ROLE_ADMIN }

 providers:
  in_memory:
      memory:
          users:
              admin: { password: 111111, roles: 'ROLE_FMTI' }

 encoders:
  Symfony\Component\Security\Core\User\User: plaintext

When I tried to access : /admin/login

I get this error : You have requested a non-existent service "request". Did you mean one of these: "router.request_context", "request_stack", "monolog.logger.request", "data_collector.request"?

Could you please help me ? Thanks in advance and sorry for my english

  • 写回答

3条回答 默认 最新

  • doushi7805 2015-12-16 12:43
    关注

    It's because you are trying to get a non existent service in your controller. In a symfony controller, you can get the request in the method call :

    In your Logincontroller, add this use statement :

    use Symfony\Component\HttpFoundation\Request;
    

    And in your function declaration

    public function loginAction(Request $request)
    {
        // you can directly use $request
        ....
    }
    

    You can now remove the line where you try to get a non existent service :

    // remove this line
    $request = $this->get('request');
    

    Also I'm not sure you will get the session with a get('session'). Based on Symfony' documentation, you should do the following :

    $session = $request->getSession();
    

    see http://symfony.com/doc/current/book/controller.html#managing-the-session

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机