1
votes

I am trying to set up users in a project, but I receive errors because of the login path. It looks like an issue with spaces, but I tried with a php file and the error is the same.

FileLoaderLoadException: The routing file "/Applications/MAMP/htdocs/Symfony/app/config/routing.yml" contains unsupported keys for "login": "pattern". Expected one of: "resource", "type", "prefix", "path", "host", "schemes", "methods", "defaults", "requirements", "options", "condition" in /Applications/MAMP/htdocs/Symfony/app/config/routing.yml (which is being imported from "/Applications/MAMP/htdocs/Symfony/app/config/routing_dev.yml").

The code in app/config was :

login:
    pattern:    /login
    defaults:   { _controller: OCUserBundle:Security:login }

EDIT:
So I get the same error with a new project.
Here is what I am doing :
-Create 2 bundles (MGeneralBundle, MUserBundle) inside "Me", the namespace of my project.
-In app/config/routing.yml:

me_m_general:
    resource: "@MeMGeneralBundle/Resources/config/routing.yml"
    prefix:   /

login:
     path:   /login
     defaults: { _controller: MeMUserBundle:Security:login}

login_check:
    path:    /login_check

logout:
    path:    /logout

And in app/config/security.yml:

security:
    in_memory:
        memory: ~
firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main_login:
    # Cette expression régulière permet de prendre /login (mais pas /login_check !)
        pattern:   ^/login$
        anonymous: true
    main:
        pattern:   ^/
        anonymous: false
        provider:  in_memory
        form_login:
            login_path: login
            check_path: login_check
        logout:
            path:   logout
            target: /

And the controller for the login, with the routing.yml in the UserBundle :

me_home:
    path:     /
    defaults: { _controller: MeMUserBundle:Default:index }



<?php

namespace Me\MUserBundle\Controller;

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

class SecurityController extends Controller
{
    public function loginAction(Request $request)
    {
      if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
        return $this->redirectToRoute('me_home');
      }
      $authenticationUtils = $this->get('security.authentication_utils');
      return $this->render('MeMUserBundle:Security:login.html.twig', array(
        'last_username' => $authenticationUtils->getLastUsername(),
        'error'         => $authenticationUtils->getLastAuthenticationError(),
      ));
    }
}

The error is :

You have requested a non-existent service "security.context".

at : http://localhost:8888/Me/web/app_dev.php/login .

I don't know how to debug this.

1
The error is pretty straight-forward. pattern is deprecated and removed for quite a while - simply replace it with path. - Artamiel
You're welcome, @Paul. Also, keep in mind that you're most likely reading an older version of the documentation. security.context has also been renamed since version 2.6. - Artamiel
@Artamiel ok thanks, I created a new project, but the error is the same. I explain in details what I am doing in the edit. - Paul
Like @Artamiel said the security.context service was deprecated in Symfony 2.6 and therefore was removed in Symfony 3.0. You need to user either the security.token_storage or security.authorization_checker service depending on what you want to do. - xabbuh
@xabbuh thanks, yes I did not understand perfectly at first the way to solve it, it works fine now, thanks. - Paul

1 Answers

0
votes

The security.context service was deprecated in 2.6 and removed entirely in 3.0. You want to use security.authorization_checker instead:

 if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {

In fact, Symfony 2.6 also introduced new controller shortcuts so you don't even have to explicitly call that service. This looks cleaner and will work just as well in your controller:

if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {