Symfony CMF admin login error

451 views Asked by At

I install symfony CMF. When i want to login as admin I get this error:

Unable to find the controller for path "/demo/login_check". The route is wrongly configured.

codes of cmf/src/Acme/DemoBundle/Resources/config/routing.yml

_demo_security:
    resource: "@AcmeDemoBundle/Controller/SecurityController.php"
    type: annotation

_demo_login_check:
    path: /login_check

_demo_logout:
    path: /logout

codes of SecurityController:

<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class SecurityController extends Controller
{
    /**
     * @Route("/login", name="_demo_login")
     * @Template
     */
    public function loginAction(Request $request)
    {
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
        }

        return array(
            'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
        );
    }
}

security controller don't have an action for login_check.

1

There are 1 answers

0
Ernest Boabramah On

In most cases symfony automatically handles the login through it's authentication listeners. So basically a form login authentication listener will handle your login when you post to /domo/login_check route. But you will have configure these things to work in the firewall section of your security.yml file in app/config/security.yml. Below is a demo file I use.

security:
providers:
    in_memory:
        memory:
            users:
                ryan:
                    password: ryanpass
                    roles: 'ROLE_USER'
                admin:
                    password: $2a$12$cyTWeE9kpq1PjqKFiWUZFuCRPwVyAZwm4XzMZ1qPUFl7/flCM3V0G
                    roles: 'ROLE_ADMIN'
    invetico.user_provider:
        entity:
            class: Bundle\UserBundle\Entity\User
            property: username
            # if you're using multiple entity managers
            # manager_name: customer
encoders:
    Bundle\UserBundle\Entity\User:
        algorithm: bcrypt
        cost: 12
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt|error)|css|images|js)/
        security: false
    default:
        pattern: ^/
        anonymous: ~
        http_basic: ~
        provider: invetico.user_provider
        form_login:
            login_path: _login
            check_path: _login_check
        logout:
            path: _logout
            target: /
        remember_me:
            key:      "%secret%"
            lifetime: 300 # 1 week in seconds
            path:     /
    # Super admin stuff
    admin_restricted_area:
        pattern:  ^/admin
        http_basic:
        provider: invetico.user_provider
access_control:
# require ROLE_ADMIN for /admin*
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/users, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/account, roles: ROLE_USER }

so as you can see under the firewall section I have configured three firewals,dev, default, admin_restricted_area.

so if you look at the default firewall , I am telling symfony to use the form_login authentication listener to handle login form display and login check. So in your case it will be

            form_login:
            login_path: _demo_login
            check_path: _demo_login_check

Now the provider section is telling symfony to use my own user provider which of cause must implement their Symfony\Component\Security\Core\User\UserProviderInterface

So you do not have to write an action for the login check route. Symfony intercept your _demo_login_check and process it automatically