I want to install KnpMenuBundle into an empty Symfony project (--webapp project).
I make steps from KnpMenuBundle documentation but it does not work.
configuration
symfony 6.1.7
KnpMenuBundle downloaded: 3.2
Here are my different files:
composer.json
{
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.13",
"knplabs/knp-menu-bundle": "^3.2",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.13",
"sensio/framework-extra-bundle": "^6.1",
"symfony/asset": "6.1.*",
"symfony/console": "6.1.*",
"symfony/doctrine-messenger": "6.1.*",
"symfony/dotenv": "6.1.*",
"symfony/expression-language": "6.1.*",
"symfony/flex": "^2",
"symfony/form": "6.1.*",
"symfony/framework-bundle": "6.1.*",
"symfony/http-client": "6.1.*",
"symfony/intl": "6.1.*",
"symfony/mailer": "6.1.*",
"symfony/mime": "6.1.*",
"symfony/monolog-bundle": "^3.0",
"symfony/notifier": "6.1.*",
"symfony/process": "6.1.*",
"symfony/property-access": "6.1.*",
"symfony/property-info": "6.1.*",
"symfony/proxy-manager-bridge": "6.1.*",
"symfony/runtime": "6.1.*",
"symfony/security-bundle": "6.1.*",
"symfony/serializer": "6.1.*",
"symfony/string": "6.1.*",
"symfony/translation": "6.1.*",
"symfony/twig-bundle": "6.1.*",
"symfony/validator": "6.1.*",
"symfony/web-link": "6.1.*",
"symfony/yaml": "6.1.*",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0"
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"symfony/flex": true,
"symfony/runtime": true
},
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.1.*"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "6.1.*",
"symfony/css-selector": "6.1.*",
"symfony/debug-bundle": "6.1.*",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^6.1",
"symfony/stopwatch": "6.1.*",
"symfony/web-profiler-bundle": "6.1.*"
}
}
config.bundles.php
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true],
];
config/packages/knp_menu.yaml
# config/packages/knp_menu.yaml
knp_menu:
# use "twig: false" to disable the Twig extension and the TwigRenderer
twig:
template: KnpMenuBundle::menu.html.twig
# if true, enables the helper for PHP templates
templating: false
# the renderer to use, list is also available by default
default_renderer: twig
src/Menu/Builder.php
<?php
// src/Menu/Builder.php
namespace App\Menu;
use App\Entity\Blog;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
final class Builder implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function mainMenu(FactoryInterface $factory, array $options): ItemInterface
{
$menu = $factory->createItem('root');
$menu->addChild('Home', ['route' => 'homepage']);
// access services from the container!
$em = $this->container->get('doctrine')->getManager();
// findMostRecent and Blog are just imaginary examples
$blog = $em->getRepository(Blog::class)->findMostRecent();
$menu->addChild('Latest Blog Post', [
'route' => 'blog_show',
'routeParameters' => ['id' => $blog->getId()]
]);
// create another menu item
$menu->addChild('About Me', ['route' => 'about']);
// you can also add sub levels to your menus as follows
$menu['About Me']->addChild('Edit profile', ['route' => 'edit_profile']);
// ... add more children
return $menu;
}
}
src/Controller/IndexController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class IndexController extends AbstractController
{
#[Route('/', name: 'app_index')]
public function index(): Response
{
return $this->render('index/index.html.twig', [
'controller_name' => 'IndexController',
]);
}
}
templates/index/index.html.twig
{% extends 'base.html.twig' %}
{% block title %}Hello IndexController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code>src/Controller/IndexController.php</code></li>
<li>Your template at <code>templates/index/index.html.twig</code></li>
</ul>
</div>
{{ knp_menu_render('App:Builder:mainMenu') }}
{% endblock %}
NOTES:
when I install KnpMenuBundle, config/packages/knp_menu.yaml did not exist, I had to create it and paste inside the code.
src/Menu folder did not exist, I had to create it with Builder.php inside and paste the code.
when I added {{ knp_menu_render('App:Builder:mainMenu') }} into my templates/index/index.html.twig, I got the error:
An exception has been thrown during the rendering of a template
("Bundle "App" does not exist or it is not enabled. Maybe you
forgot to add it in the "registerBundles()" method of your
"App\Kernel.php" file?").
If you have any ideas, clues, or anything else, I'm listening because I don't know where the mistake is.
I am using method b: a menu builder as a service without any problems.
Look at the documentation page. So I recommend you do the same way.
Related
My form works but auto-complete does not.
Anyone has an idea of what could be wrong?
Thanks and sorry for my english!
I tried adding "autocomplete" attribute:
<?php
namespace App\Form;
use App\Entity\Category;
use App\Entity\City;
use App\Entity\Location;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username', TextType::class,[
'label'=>'Pseudo'
])
[...]
->add('city', EntityType::class,[
'class'=>City::class,
'choice_label'=>'name',
'label'=>'Ville',
'placeholder'=>'Selectionnez votre ville',
**'autocomplete'=>true**
]
)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
As a result, my form still works, but there is no autocomplete:
I tried using AutocompleteField instead:
**->add('city',CityAutocompleteField::class)**
<?php
namespace App\Form;
use App\Entity\City;
use App\Repository\CityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\ParentEntityAutocompleteType;
#[AsEntityAutocompleteField]
class CityAutocompleteField extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class'=>City::class,
'choice_label'=>'name',
'placeholder'=>'Saisissez',
'query_builder' => function(CityRepository $cityRepository) {
return $cityRepository->createQueryBuilder('city');
},
//'security' => 'ROLE_SOMETHING',
]);
}
public function getParent(): string
{
return ParentEntityAutocompleteType::class;
}
}
The result is différent. I have the value stored in my database, but that's all. I can't select another value:
I followed the instructions given here( https://symfony.com/bundles/ux-autocomplete/current/index.html ), but don't why it is not ok here.
code in my browser:
`<select id="looking_for_city" name="looking_for[city]" required="required" data-controller="symfony--ux-autocomplete--autocomplete" data-symfony--ux-autocomplete--autocomplete-no-results-found-text-value="No results found" data-symfony--ux-autocomplete--autocomplete-no-more-results-text-value="No more results"><option value="" selected="selected">Selectionnez votre ville</option><option value="25000">Boudes(63)</option><option value="25001">La Bourboule(63)</option>...<option value="35853">Île de Clipperton(989)</option></select>`
controller.json:
{
"controllers": {
"#symfony/ux-autocomplete": {
"autocomplete": {
"enabled": true,
"fetch": "eager",
"autoimport": {
"tom-select/dist/css/tom-select.default.css": true
}
}
}
},
"entrypoints": []
}
composer.json:
{
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"ext-ctype": "*",
"ext-iconv": "*",
"beberlei/doctrineextensions": "^1.3",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.13",
"knplabs/knp-paginator-bundle": "^5.9",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.8",
"sensio/framework-extra-bundle": "^6.1",
"symfony/apache-pack": "^1.0",
"symfony/asset": "5.4.*",
"symfony/console": "5.4.*",
"symfony/doctrine-messenger": "5.4.*",
"symfony/dotenv": "5.4.*",
"symfony/expression-language": "5.4.*",
"symfony/flex": "^1.17|^2",
"symfony/form": "5.4.*",
"symfony/framework-bundle": "5.4.*",
"symfony/http-client": "5.4.*",
"symfony/intl": "5.4.*",
"symfony/mailer": "5.4.*",
"symfony/mime": "5.4.*",
"symfony/monolog-bundle": "^3.0",
"symfony/notifier": "5.4.*",
"symfony/process": "5.4.*",
"symfony/property-access": "5.4.*",
"symfony/property-info": "5.4.*",
"symfony/proxy-manager-bridge": "5.4.*",
"symfony/runtime": "5.4.*",
"symfony/security-bundle": "5.4.*",
"symfony/serializer": "5.4.*",
"symfony/string": "5.4.*",
"symfony/translation": "5.4.*",
"symfony/twig-bundle": "5.4.*",
"symfony/ux-autocomplete": "^2.4",
"symfony/validator": "5.4.*",
"symfony/web-link": "5.4.*",
"symfony/webpack-encore-bundle": "^1.16",
"symfony/yaml": "5.4.*",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4",
"fakerphp/faker": "^1.20",
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "5.4.*",
"symfony/css-selector": "5.4.*",
"symfony/debug-bundle": "5.4.*",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^6.1",
"symfony/stopwatch": "5.4.*",
"symfony/web-profiler-bundle": "5.4.*"
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"symfony/flex": true,
"symfony/runtime": true
},
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.4.*"
}
}
}
package.json:
{
"devDependencies": {
"#babel/core": "^7.17.0",
"#babel/preset-env": "^7.16.0",
"#hotwired/stimulus": "^3.0.0",
"#symfony/stimulus-bridge": "^3.2.0",
"#symfony/ux-autocomplete": "file:vendor/symfony/ux-autocomplete/assets",
"#symfony/webpack-encore": "^4.0.0",
"core-js": "^3.23.0",
"regenerator-runtime": "^0.13.9",
"tom-select": "^2.0.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-notifier": "^1.15.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
}
}
I had the same problem, or at least it was very similar. I solved it by adding the following code before </head> in templates\base.html.twig:
<!--Symfony UX -->
{{ encore_entry_link_tags('app') }}
{{ encore_entry_script_tags('app') }}
I hope this helps.
I upgraded my API Platform project from v. 2.7 to v. 3.
Using the version 2.7 I was able to run my unit tests without problems. But now after upgrading to version 3, I receive the following error:
Error : Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '256' frames
Console output:
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/Compiler/ResolveNoPreloadPass.php:81
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/Compiler/AbstractRecursivePass.php:91
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/Compiler/ResolveNoPreloadPass.php:92
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/Compiler/ResolveNoPreloadPass.php:40
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/Compiler/Compiler.php:73
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/dependency-injection/ContainerBuilder.php:716
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/http-kernel/Kernel.php:538
PhpstormProjects/Members/lloyd-members-api/vendor/symfony/http-kernel/Kernel.php:767
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/http-kernel/Kernel.php:128
/PhpstormProjects/Members/lloyd-members-api/vendor/symfony/framework-bundle/Test/KernelTestCase.php:72
/PhpstormProjects/Members/lloyd-members-api/vendor/api-platform/core/src/Symfony/Bundle/Test/ApiTestCase.php:49
/PhpstormProjects/Members/lloyd-members-api/tests/EasyTest.php:16
I simplified the test to the minimum:
<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
class EasyTest extends ApiTestCase
{
public function testSomething(): void
{
$this->assertTrue(true);
}
public function testProblem(): void
{
self::createClient();
$this->assertTrue(true);
}
}
testSomething() runs as it should.
testProblem() results in the error.
This is my composer.json
{
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-curl": "*",
"ext-exif": "*",
"ext-gd": "*",
"ext-iconv": "*",
"api-platform/core": "^3.0",
"composer/package-versions-deprecated": "1.11.99.4",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.13",
"imagine/imagine": "^1.3",
"jonasarts/phpqrcode-bundle": "^6.0",
"lexik/jwt-authentication-bundle": "^2.15",
"nelmio/cors-bundle": "^2.2",
"phpdocumentor/reflection-docblock": "^5.3",
"symfony-bundles/json-request-bundle": "^4.0",
"symfony/apache-pack": "^1.0",
"symfony/asset": "6.1.*",
"symfony/console": "6.1.*",
"symfony/dotenv": "6.1.*",
"symfony/expression-language": "6.1.*",
"symfony/flex": "^2",
"symfony/form": "6.1.*",
"symfony/framework-bundle": "6.1.*",
"symfony/http-client": "6.1.*",
"symfony/mailer": "6.1.*",
"symfony/monolog-bundle": "^3.7",
"symfony/property-access": "6.1.*",
"symfony/property-info": "6.1.*",
"symfony/proxy-manager-bridge": "6.1.*",
"symfony/requirements-checker": "^2.0",
"symfony/runtime": "6.1.*",
"symfony/security-bundle": "6.1.*",
"symfony/serializer": "6.1.*",
"symfony/twig-bundle": "6.1.*",
"symfony/uid": "6.1.*",
"symfony/validator": "6.1.*",
"symfony/yaml": "6.1.*",
"twig/cssinliner-extra": "^3.3",
"twig/extra-bundle": "^3.3",
"twig/twig": "^2.12|^3.0",
"vich/uploader-bundle": "^1.19"
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"symfony/flex": true,
"symfony/runtime": true
}
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd",
"requirements-checker": "script"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.1.*"
}
},
"require-dev": {
"hautelook/alice-bundle": "^2.10",
"justinrainbow/json-schema": "^5.2",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.12.21",
"roave/security-advisories": "dev-latest",
"symfony/browser-kit": "6.1.*",
"symfony/css-selector": "6.1.*",
"symfony/debug-bundle": "6.1.*",
"symfony/maker-bundle": "^1.36",
"symfony/phpunit-bridge": "^6.1",
"symfony/stopwatch": "6.1.*",
"symfony/var-dumper": "6.1.*",
"symfony/web-profiler-bundle": "6.1.*"
}
}
Composer is up to date (composer up).
Composer recipes are all up to date.
Can't find the problem. Removed vendor folder. Same problem.
I can run the tests with the following flag:
-dxdebug.mode=debug
In this case everything works fine.
Thank you!
Since I updated Symfony 4.4 to symfony 5.4, i have a last problem to resolve.
Methode "generateURl()" in controller, and "path()" in twig template return an exception for route with optional param that i dont specify for generateURl method in controller or path methode in twig:
Some mandatory parameters are missing ("optionalvarname") to generate a URL for
route "the_route".
Otherwise routes themselves work well. I can connect on its without specifie the defaut param.
Everythings worked well in symfony 4.4
But in Symfony 5.4 I can't omit to specify optional param for this two methods.
Exemple with an "action" optional parameters :
/**
* #Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
*/
public function edit(
Request $request, Vente $vente,string $action="update",Panier $panier,
MouvementRepository $mouvementRepository,VenteUtils $venteUtils): Response
{
.....
$this->generateUrl('tenant_vente_edit',['id'=>$vente->getId()])
I omited "action" optional param and this generate exception : Some mandatory parameters are missing ("action") to generate a URL for route "tenant_vente_edit".
But the route itslef works...when there is no action parameter in url.
To "generateUrl()" and "path()" works omiting optionals parameters i have to add the defaut value "?update" in the comment :
/**
* #Route("/{id}/edit/{action?update}", name="tenant_vente_edit", methods={"GET","POST"})
*/
public function edit(
Request $request, Vente $vente,string $action="update",Panier $panier,
MouvementRepository $mouvementRepository,VenteUtils $venteUtils): Response
{
In this case i have no error for "generateUrl()" method and "path()" method.
Would you know why i have this behviour ? I don't want to go back on all my route...I have a lot..
Thank you very much for your help and sorry for my english....
My new composer.json for update to 5.4
{
"type": "project",
"license": "proprietary",
"require": {
"php": ">=8.1.0",
"ext-ctype": "*",
"ext-iconv": "*",
"beberlei/doctrineextensions": "^1.3",
"composer/package-versions-deprecated": "1.11.99.4",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.11",
"knplabs/knp-paginator-bundle": "^5.8",
"knplabs/knp-snappy-bundle": "^1.7",
"phpdocumentor/reflection-docblock": "^5.3",
"phpoffice/phpspreadsheet": "^1.14",
"sensio/framework-extra-bundle": "^6.1",
"symfony/asset": "5.4.*",
"symfony/console": "5.4.*",
"symfony/dotenv": "5.4.*",
"symfony/expression-language": "5.4.*",
"symfony/flex": "^1.17|^2",
"symfony/form": "5.4.*",
"symfony/framework-bundle": "5.4.*",
"symfony/monolog-bundle": "^3.1",
"symfony/process": "5.4.*",
"symfony/property-access": "5.4.*",
"symfony/property-info": "5.4.*",
"symfony/proxy-manager-bridge": "5.4.*",
"symfony/security-bundle": "5.4.*",
"symfony/serializer": "5.4.*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/translation": "5.4.*",
"symfony/twig-bundle": "5.4.*",
"symfony/validator": "5.4.*",
"symfony/web-link": "5.4.*",
"symfony/webpack-encore-bundle": "^1.5",
"symfony/yaml": "5.4.*",
"vich/uploader-bundle": "^1.19",
"twig/extra-bundle": "^2.12",
"twig/twig": "^2.12"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "5.4.*",
"symfony/css-selector": "5.4.*",
"symfony/debug-bundle": "5.4.*",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^5.3",
"symfony/stopwatch": "5.4.*",
"symfony/web-profiler-bundle": "5.4.*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"allow-plugins": {
"symfony/flex": true
}
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.4.*"
}
}
}
it seams optional paraméters must be at the end of controller arguments.
In this two cases routes work if you omit optional parameter in url.
First case :
/**
* #Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
*/
public function edit(
Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
VenteUtils $venteUtils,?string $action=null): Response
{
Second case
/**
* #Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
*/
public function edit(
Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
?string $action=null, VenteUtils $venteUtils): Response
{
But :
$this->generateUrl('tenant_vente_edit',['id'=>$vente->getId()])
Works only in the second case with "action" optional argument in last position of controller arguments, idem for twig and "path()".
I didnt have this behaviour in symfony 4.4
So no other choice to check all my routes. To avoid this kind of problem i advice to put default value in comment instead of controller arguments.
/**
* #Route("/{id}/edit/{action?}", name="tenant_vente_edit", methods={"GET","POST"})
*/
public function edit(
Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
VenteUtils $venteUtils,?string $action): Response
action?(_blank) for null value in this example.
My application is on Symfony 4.4, PHP 7.4.
My composer.json :
{
"name": "saro0h/to-do-list",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11",
"doctrine/annotations": "^1.11",
"doctrine/doctrine-bundle": "^2.1",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.7",
"sensio/framework-extra-bundle": "^5.6",
"symfony/apache-pack": "^1.0",
"symfony/asset": "4.4.*",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/form": "4.4.*",
"symfony/framework-bundle": "4.4.*",
"symfony/mailer": "4.4.*",
"symfony/monolog-bundle": "^3.6",
"symfony/security-bundle": "4.4.*",
"symfony/security-csrf": "4.4.*",
"symfony/twig-bundle": "4.4.*",
"symfony/validator": "4.4.*",
"symfony/yaml": "4.4.*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.54",
"symfony/browser-kit": "4.4.*",
"symfony/css-selector": "4.4.*",
"symfony/maker-bundle": "^1.23",
"symfony/phpunit-bridge": "^5.1",
"symfony/stopwatch": "^4.4",
"symfony/web-profiler-bundle": "^4.4"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}
There is the method controller I want to test:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserController extends AbstractController
{
/**
* list all users.
*
* #Route("/users", name="user_list")
*
* #return Response
*/
public function list(): Response
{
return $this->render('user/list.html.twig', ['users' => $this->getDoctrine()->getRepository(User::class)->findAll()]);
}
}
Then a test it with PHPUnit
<?php
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* #internal
* #coversNothing
*/
class UserControllerTest extends WebTestCase
{
/**
* testIndex.
*/
public function testUserList(): void
{
$client = static::createClient();
$client->request('GET', '/users');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}
In the browser, the request is successfull (The page display without error, with 200 HTTP code status, no errors in the profiler)
But, when I run PHPUnit, the test fails with $client->getResponse()->getStatusCode() = 500 !!!!
How can I fix that ?
Thank you for your answers
I solved my problem !
When a had the idea to display the page with the test environment, I get the error:
An exception occurred in driver: SQLSTATE[HY000] [2002] Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée.
So I solved the problem creating a database for the test environment, in defined my .env.test.
I'm working on a Symfony 4 project with PhpStorm 2018.1
It has become common for me to see the use declarations highlighted as if they haven't been installed with composer.
The class belongs to a package which is not directly required in your
composer.json. Please add the package into your composer.json.
The following are installed and show in my composer.json file:
stof/doctrine-extensions-bundle
symfony/orm-pack
Am I missing something here or is this a PhpStorm issue?
composer.json
{
"type": "project",
"license": "proprietary",
"platform": {
"php": "7.1"
},
"require": {
"php": "^7.1",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"stof/doctrine-extensions-bundle": "^1.3",
"symfony/asset": "^4.1",
"symfony/console": "^4.1",
"symfony/expression-language": "^4.1",
"symfony/flex": "^1.0",
"symfony/form": "^4.1",
"symfony/framework-bundle": "^4.1",
"symfony/lts": "^4#dev",
"symfony/maker-bundle": "^1.5",
"symfony/monolog-bundle": "^3.3",
"symfony/orm-pack": "^1.0",
"symfony/process": "^4.1",
"symfony/profiler-pack": "^1.0",
"symfony/security-bundle": "^4.1",
"symfony/security-guard": "^4.1",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/twig-bundle": "^4.1",
"symfony/validator": "^4.1",
"symfony/var-dumper": "^4.1",
"symfony/web-link": "^4.1",
"symfony/webpack-encore-pack": "^1.0",
"symfony/yaml": "^4.1"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0",
"symfony/debug-pack": "*",
"symfony/dotenv": "^4.1",
"symfony/test-pack": "^1.0",
"symfony/web-server-bundle": "^4.1"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": true
}
}
}
What this message is telling you is that the highlighted classes are only available because the packages containing them have been installed as dependencies of some other packages you require (in this case it's probably because of the symfony/orm-pack package). Generally, relying on such transitive dependencies is not something I would do. Though for the Symfony pack that's something acceptable IMO.
If you are still concerned about this message, you can get rid of it by unpacking symfony/orm-pack (see also http://fabien.potencier.org/symfony4-unpack-the-packs.html):
$ composer unpack symfony/orm-pack
Your import path is correct, your composer.json looks good also.
Try to delete the vendor directory then try to launch composer install.
Try to install Symfony Plugin in your PHPStorm.
EDIT
Do you have the line below in your config/bundles.php ?
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],