简体   繁体   中英

Unable to generate a URL for the named route “login” as such route does not exist

I have just got my head around the basics of symfony and the fos bundle.

I am now trying to create a link in the header of a page that either reads 'log in' or 'log out' based on whether the user is logged in or not.

The links on my app to log in / out are (the out of the box defaults: "/login" & "/logout"

However i cannot seem to pass these values to the path function in twig:

{% if app.user %}
        <li><a href="{{ path('logout') }}">Log Out</a></li>
{% else %}
        <li><a href="{{ path('login') }}">Log In</a></li> 
{% endif %}

I get the error:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "login" as such route does not exist.") in BloggerBlogBundle:Page:index.html.twig at line 10. 

How can I pass the login and logout to path in twig so it works correctly in both dev and prod environment?

在命令行上,如果键入“ php app / console route:debug”,则应获得所有可用路由的列表。

And found my own answer.. should be using the FOS bundle duh!

    {% if app.user %}
        <li><a href="{{ path('fos_user_security_logout') }}">Log Out</a></li>
{% else %}
        <li><a href="{{ path('fos_user_security_login') }}">Log In</a></li> 
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM