PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label symfony6. Show all posts
Showing posts with label symfony6. Show all posts

Friday, September 2, 2022

[FIXED] How can I go to the login page ? (Symfony 6.0.2)

 September 02, 2022     authentication, path, routes, symfony, symfony6     No comments   

Issue

I created a route and named it login. So when I created the navbar. I added href="{{ path('login') }}" 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=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text></svg>">

        {# Bootstrap #}

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/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 class="fixed-top">
        <nav class="navbar navbar-expand-lg bg-light">

            <!-- Home page -->

            <a class="navbar-brand" href="{{ path('home') }}">
                <img src="/docs/5.2/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24" class="d-inline-block align-text-top">
            </a>

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

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

            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto px-4">

                    <!-- Connexion -->

                    <li class="nav-item">
                        <a class="nav-link" href="{{ path('login') }}">Se connecter</a>
                    </li>

                    <!-- Inscription -->

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

    </body>
</html>

Solution

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



Answered By - Holicz
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, March 12, 2022

[FIXED] Change Twig Loader order

 March 12, 2022     php, symfony, symfony6, twig     No comments   

Issue

So I've created a few twig loaders in my project for templates stored in databases. Simply having them implement LoaderInterface already automatically adds them to the default Twig\Loader\ChainLoader - however, I haven't found a way to configure the order.

When I check which loaders are in the ChainLoader, it's

LoaderA -> LoaderB -> FilesystemLoader

, but I want the order to be

FilesystemLoader -> LoaderB -> LoaderA

If I define twig.loader in my services.yaml, it always ends up with infinite recursion and nothing works. If I manually configure the calls to twig.loader.chain, I get the correct order of loaders, followed by a chainloader X, followed by the loaders in the "autoconfigured" order. Chainloader X is the same as the very ChainLoader Symfony configures, so... infinite recursion if a template is not found.

So how on earth do I tell twig which loaders to load in what order? Do I need to create a CompilerPass just for this simple requirement?


Solution

You can alter the order by tagging the services manually, when priority is not specified it defaults to 0:

# services.yaml
    App\Twig\LoaderA:
        tags:
            - { name: 'twig.loader', priority: 2 }
    App\Twig\LoaderB:
        tags:
            - { name: 'twig.loader', priority: 1 }

If you prefer, since Symfony 5.3 (running under PHP 8), you can use PHP attributes instead.



Answered By - msg
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing