PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 9, 2022

[FIXED] Install twig/extensions on a Symfony 4 or Symfony 5 new project

 February 09, 2022     composer-php, symfony, symfony4, twig, twig-extension     No comments   

Issue

I'm preparing to migrate a huge website from Symfony 3.4 to Symfony 4.4. To do this, I'm starting from a new fresh installation of Symfony 4.4, and as the intial project requires the use of Twig Extensions I try to install it on this new Symfony 4.4 project. But composer indicates Your requirements could not be resolved to an installable set of packages.

The error message is obvious, but I don't understand why this situation happens on a fresh symfony 4.4 project.

What I tried :

  • I create a new Symfony symfony new --full --version=lts testproject that installed Symfony 4.4 and right after composer require twig/extensions => Your requirements could not be resolved to an installable set of packages.

  • I create a new Symfony symfony new --full testproject that installed Symfony 5.0 and right after composer require twig/extensions => Your requirements could not be resolved to an installable set of packages.

  • I tried with Symfony flex but same problem => Your requirements could not be resolved to an installable set of packages.

    • I retried after clearing the composer cache but no change.

This worked : - I create a new Symfony symfony new --full --version=3.4 testproject that installed Symfony 3.4 and right after composer require twig/extensions => OK

I understand that dependencies conflits occur for Symfony 4.4 and more, but according to Symfony doc How to Write a custom Twig Extension no further actions are required and it should work.

What I'm missing ? Does someone faced the same problem ? Thanks


Solution

Finally it seems that running composer require twig/extensions is no more required to create custom Twig Extensions in Symfony 4. By default a new Symfony built with website skeleton owes a use Twig\Extension\AbstractExtension; as per required to create a twig extension like below one :

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
    public function getFilters()
    {
        return [
            new TwigFilter('price', [$this, 'formatPrice']),
        ];
    }

    public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
    {
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
        $price = '$'.$price;

        return $price;
    }
}


Answered By - Grégory C
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

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
Comments
Atom
Comments

Copyright © PHPFixing