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

在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条)

相关推荐 更多相似问题