Search code examples
symfonyroutessymfony-routingsymfony-3.2

Symfony : Issue with routing


I'm new to Symfony, I follow a tutorial, the part about security and user management but I'm stucked with a problem that seems to come from my routing.

I just created a login form that is actually working, when I go on /app_dev.php/login, the form shows up, I can fill it, but when I submit it, I got the following error :

No route found for "GET /" (from "http://dev-05/ANTOINE/Symfony/web/app_dev.php/login")
404 Not Found - NotFoundHttpException
1 linked Exception:
ResourceNotFoundException » 

After getting this error, if I go back on the home page, I can see I am connected, so it's working, but the redirection is not .

According to the documentation, this comes from the routing that might be wrongly configured, but I don't know where I made a mistake.

Here's my form, my security.yml and my routing.yml files :

{% extends "AKMUserBundle::layout.html.twig" %}

{% block akmuser_body %}

{% if error %}
    <div class="alert alert-danger">{{ error.message }}</div>
{% endif %}

<form action="{{ path('login_check') }}" method="post">
    <label for="username">Login : </label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Mot de passe :</label>
    <input type="password" id="password" name="_password" />
    <br />
    <input type="submit" value="Connexion" />
</form>

{% endblock %}

security.yml :

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

    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        in_memory:
            memory: 
                users:
                    user: { password: userpass, roles: ['ROLE_USER'] }
                    admin: { password: adminpass, roles: ['ROLE_ADMIN'] }

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern: ^/
            anonymous: true
            form_login:
                login_path: login
                check_path: login_check
            logout:
                path: logout
                target: /platform

routing.yml :

akm_platform:
resource: "@AKMPlatformBundle/Resources/config/routing.yml"
prefix:   /platform

login:
    path: /login
    defaults:
        _controller: AKMUserBundle:Security:login

login_check:
    path: /login_check

logout:
    path: /logout

I'm aware that .yml files are very sensitive and need 4 spaces instead of the usual indentation, so I rewrote the files line by line, with the spaces, but it is still not working.

I hope someone can help me :p If you need some informations don't hesitate!

Edit : Here is my result of the php bin/console debug:router php bin/console debug:router

Edit 2 : To get rid of my problem I just had to add the default_target_path in my security.yml :

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

    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        in_memory:
            memory: 
                users:
                    user: { password: userpass, roles: ['ROLE_USER'] }
                    admin: { password: adminpass, roles: ['ROLE_ADMIN'] }

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern: ^/
            anonymous: true
            form_login:
                login_path: login
                check_path: login_check
                default_target_path: akm_platform_home
            logout:
                path: logout
                target: /platform

Solution

  • Refresh your cache, console command:php bin/console cache:clear, if you are using older symfony it's app/console instead of bin/console. You can debug your routes with a command: php bin/console debug:router. This is the main system console and in my case I use it on Windows. You must be in the project folder for them to work.

    I am not sure where you get redirected to "/", i recently started working in Symfony and most issues were with refreshing cache and wrong yml and route names. However in your case it may be that symfony goes to route / on successful login, you can add default_target_path: your_homepage_route_name or /where_you_want_to_go it may be what's the issue here.

    Since you're new, when you include routes and define a prefix for them, you can easily forget that you set it, which is why router debugging is great since you can see all the info there very easily. When working in symfony always have a console window open if not working in an edior with a built in console. I think JetBeans has it, all of those tutorials are done in it. PS, youtube tutorials for symfony are great, for example Symfony and PHP Programming channel has a good beginner tutorial.