1

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
AKMMM
  • 294
  • 1
  • 2
  • 19
  • In your browser view the source of your login form and verify that the method is POST and the action is /login_check. The error message implies you have GET / (Off topic: yaml does not require 4 spaces, just need to be consistent). – Cerad Apr 14 '17 at 13:29
  • 1st is to create a vhost that points to the /web directory of your project. Does the / route works ? http://dev-05/ANTOINE/Symfony/web/app_dev.php – COil Apr 14 '17 at 13:33
  • I edited my post so you can see my form, it seems to be ok for me. Concerning the routes, if I try to go to /web/app_dev.php it doesn't work, but with a "/" at the end it works fine, do you think the issue comes from here? – AKMMM Apr 14 '17 at 13:38

1 Answers1

1

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.

  • I already refreshed the cache, I did it again, just in case, didn't change anything. I add the results of `php bin/console debug:router` in the original post, as I see it, everything seem normal – AKMMM Apr 14 '17 at 14:06
  • Mhm, interesting. Have you tried setting the default target path, I edited my post with info on it, also, don't forget to save any changes you've made in your editor, I've been down that road before :) – Marko Živković Apr 14 '17 at 14:16
  • OK, I read the symfony [link](http://symfony.com/doc/current/security/form_login.html) documentation and It seems you always get redirected upon a successful login, since you didn't define the default target path it is trying to redirect you to the requested route which is /login and since you already where there it probably tries to go to / which is defined in the skeleton application itself but you probably altered it or deleted the default AppBundle controller. – Marko Živković Apr 14 '17 at 14:21
  • Thank you, it was that, I just added the default_target_path on my form_login (security.yml), and it works just fine ! :D – AKMMM Apr 14 '17 at 14:32
  • Ah sweet, glad I could help. Check out the youtube tutorials on symfony they can help quite a bit and don't forget that production enviroment cache has to be cleared separetely. I switched to prod and lost hours on a silly "hello world" page banging my head on the table and thinking up what is wrong. Also, if there is a bug in validation.yml, you'll learn about it soon, you can see those errors too when you refresh your cache, sometimes, other times, it will require good old banging your head on the table. I hate debugging YML – Marko Živković Apr 14 '17 at 14:41
  • I'll go check on youtube if you say there are great tutorials :p Symfony looks very powerful, but it needs some time to get used to it :p And yes, yml file already made me crazy a bit – AKMMM Apr 14 '17 at 15:15