Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Request and Session in Symfony 3 Controller

Tags:

php

symfony

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

like image 518
Harea Costicla Avatar asked Nov 27 '22 20:11

Harea Costicla


1 Answers

Instead of this

$this->get('request')

Use

$this->get('request_stack')->getCurrentRequest()
like image 191
GarryOne Avatar answered Nov 30 '22 08:11

GarryOne