Home > Software engineering >  How can I go to the login page ? (Symfony 6.0.2)
How can I go to the login page ? (Symfony 6.0.2)

Time:08-30

I created a route and named it login. So when I created the navbar. I added href="{{ path('home') }}" for the link that leads to the login page. But when I click on the link, it doesn't lead me to the login page. I stay on the home page.

Where can be the problem ? Thank you for your help. :)

Code in SecurityController.php file

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
    #[Route(path: '/login', name: 'login')]
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        // if ($this->getUser()) {
        //     return $this->redirectToRoute('target_path');
        // }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
    }

    #[Route(path: '/logout', name: 'logout')]
    public function logout(): void
    {
        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
    }
}

Code in base.html.twig file

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}{% endblock %}</title>
        <link rel="icon" href="data:image/svg xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128"><text y="1.2em" font-size="96">⚫️</text></svg>">

        {# Bootstrap #}

        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">

        {# Google fonts #}

        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> 
        <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
        
        {% block stylesheets %}
            {{ encore_entry_link_tags('app') }}
        {% endblock %}

        {% block javascripts %}
            {{ encore_entry_script_tags('app') }}
        {% endblock %}
    </head>
    <body>

    <header >
        <nav >

            <!-- Home page -->

            <a  href="{{ path('home') }}">
                <img src="/docs/5.2/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24" >
            </a>

            <!-- Bouton de déploiement -->

            <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span ></span>
            </button>

            <div  id="navbarNav">
                <ul >

                    <!-- Connexion -->

                    <li >
                        <a  href="{{ path('login') }}">Se connecter</a>
                    </li>

                    <!-- Inscription -->

                    <li >
                        <a  href="#">S'inscrire</a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>
        
        {% block main %}{% endblock %}

    </body>
</html>

CodePudding user response:

You are generating path for homepage. Change href="{{ path('home') }}" to href="{{ path('login') }}".

CodePudding user response:

Your 'home' path is targeting the homepage, you should replace it by 'login' path.

If you need to protect the 'home' route by authentication, you can creates some access control rules: https://symfony.com/doc/current/security/access_control.html

  • Related