Symfony2: KnpMenuBundle: The menu "main" is not defined exception in rendering template - symfony

I'm using Symfony2 with the KnpMenuBundle installed as submodules and using the service configuration method.
When calling the page with the menu in the layout using:
{{ knp_menu_render('main') }}
I get the error:
An exception has been thrown during the rendering of a template ("The
menu "main" is not defined.") in CCGlobalBundle::layout.html.twig
My services.yml file (is parsed in the load() method of the CCGlobalExtension.php bundle class) seems to be being called as intentional errors in it cause further code errors:
# src/CC/GlobalBundle/Resources/Config/services.yml
services:
cc_global.menu_builder:
class: CC\GlobalBundle\Menu\MenuBuilder
arguments: ["#knp_menu.factory"]
cc_global.menu.main:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory_service: cc_global.menu_builder
factory_method: createMainMenu
arguments: ["#request"]
scope: request # needed as we have the request as a dependency here
tags:
- { name: knp_menu.menu, alias: main }
And my main /app/config/config.yml file contains the knp_menu: directive:
knp_menu:
twig:
template: knp_menu.html.twig
templating: false # if true, enables the helper for PHP templates
default_renderer: twig # The renderer to use, list is also available by default
There is the same question but no specific solution that worked here: Symfony2 - KnpMenuBundle: Fatal exception in service container
Does anyone know what could be the problem (also checked the Git issues) for this?

I ran into this issue, but found this exception "The menu 'main' is not defined" was thrown because I had not registered the service of MyCompany/MyBundle/Resources/config/services.yml in app/config/config.yml. Fixed like this:
http://symfony.com/doc/current/book/service_container.html
# app/config/config.yml
imports:
- { resource: "#MyCompanyMyBundle/Resources/config/services.yml" }

A code error was introduced to the Git. The git issue is here: https://github.com/KnpLabs/KnpMenuBundle/issues/89
The change for others looking was line 28 of the /vendor/bundles/Knp/Bundle/MenuBundle/DependencyInjection/KnpMenuExtension.php should have read:
$container->getDefinition(sprintf('knp_menu.menu_provider.%s', $builder))->addTag('knp_menu.provider');
while the incorrect code read:
$container->getDefinition(sprintf('knp_menu.menu_provider.%s', $builder))->addTag('knp_menu.menu_provider');
Note 'provider' vs. '*menu_provider*'

Related

Unused binding error when trying to bind logger to autowired controller constructor in Symfony 3.4

After upgrading to Symfony 3.4 from 2.8, I am attempting to get rid of warnings about using services from the container. One hang up is my controller all extend from an abstract controller which needs access to the monolog logger. I've decided to use autwiring for my controllers and have added a constructor in the base controller which has a LoggerInterface $logger as the only argument. In attempt to configure this once, I've added the $logger variable with a reference to the logger service under the bind section of services.yml.
However, I keep getting the error:
Unused binding "$logger" in service "security.authentication.failure_handler.secured_area.form_login"
I believe this error is supposed to appear only if no services have a constructor argument with that variable name. Now I know that my controllers all have this in the abstract class, as well as being part of some of my other services, so this seems wrong. How can I get rid of this error?
Here is what my services.yml looks like:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$logger: "#logger"
$env: "%sys_env%"
_instanceof:
\Twig_Extension:
tags: ['twig.extension']
AppBundle\:
resource: '../../../../../../src/AppBundle/{Controller,Service,Twig}/*'
exclude: '../../../../../../src/AppBundle/Service/Exception/*'
# SECURITY ########################################################################
security.authentication.failure_handler:
class: AppBundle\Security\AuthenticationFailureHandler
autowire: false
arguments: ["#http_kernel", "#security.http_utils", {}, "#app.service.security", "#doctrine.orm.entity_manager", "#logger"]
tags:
- { name: 'monolog.logger', channel: 'security' }
UPDATE 1:
I noticed that in security.authentication.failure_handler I have a reference to one of my services: app.service.security. I forgot to declare that below, so I added the following to services.yml:
app.service.security:
class: AppBundle\Service\SecurityService
That got rid of the logger error, however now I'm seeing an error about the $env string variable:
Unused binding "$env" in service "security.authentication.failure_handler.secured_area.form_login".
I'm concerned that the error message is not the real error, and this is a red herring. The bind options seem a little flaky. Any advice appreciated...
UPDATE 2:
I've decided to get rid of the bind and instanceof config and am setting up the values manually, but now this is the error: Cannot autowire service "app.service.security": argument "$sysInfoService" of method "AppBundle\Service\SecurityService::__construct()" references class "AppBundle\Service\SystemInfoService" but no such service exists. You should maybe alias this class to the existing "app.service.system_info" service.
What's weird is that I believe I'm doing exactly what the error is suggesting to do; I've added aliases for the supposedly autowired service:
app.service.system_info:
class: AppBundle\Service\SystemInfoService
app.service.security:
class: AppBundle\Service\SecurityService
I do have some services which I manually declare with autowired: false in order to manually set the arguments. That should be ok, I think; you should be able to have autowired and manual wiring coexisting in the service container, right?

Symfony2: overriding Sensio FrameworkExtraBundle template guesser

I am trying to override the standard template guesser ( located in Sensio\Bundle\FrameworkExtraBundle\Templating; ) becouse I want to use annotations to set the view but need to change the logic of how the actual view file is chosen.
I have seen this: https://github.com/elnur/ElnurTemplateGuesserBundle
but I was wondering if there is a way to just override the service in configuration.
I tried setting:
services:
sensio_framework_extra.view.guesser:
class: myCompany\myBundle\Templating\TemplateGuesser
but I get:
ContextErrorException: Catchable Fatal Error:
Argument 1 passed to Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser::__construct()
must implement interface Symfony\Component\HttpKernel\KernelInterface, none given
Am I supposed to set an argument in the service config setting? But how do I reference the HttpKernel?
Or am I missing something?
TIA.
You can inject the kernel the same way the as the original TemplateGuesser. The name of the Kernel service is just simply kernel.
services:
sensio_framework_extra.view.guesser:
class: myCompany\myBundle\Templating\TemplateGuesser
arguments: [ "#kernel" ]
To see a full list of services in the container, run
$ php app/console container:debug
Of which you'll see the kernel listed as one of them.

Assetic Route Not Found

I have a twig extension whose purpose is to collect a list of CSS and JS file paths given to it by function calls throughout a template hierarchy and then at the end of the twig template to take the output buffer and include these files in the <head> section of the page. For the most part it has been straightforward to implement.
In my service definition for the twig extension I am injecting the assetic.helper.dynamic service into it. The problem is when I call the javascripts() or stylesheets() method to get a URL for a CSS or JS file I get an error like this:
An exception has been thrown during the rendering of a template ("None
of the chained routers were able to generate route: Route
'_assetic_bd311c7' not found")
service.yml:
admin.twig.asset_extension:
class: Zing\Delta\AdminBundle\Twig\AssetExtension
tags:
- { name: twig.extension }
arguments: ['#assetic.helper.dynamic']
In my extension I am essentially doing this to get the URL for an asset:
$assetic_helper->stylesheets(array(
'#SomeBundle/Resources/public/js/jquery.tablesort.min.js'
));
I don't understand why the router can't find the routes or why assetic is setting up the routes.
The fix ended up being to run the following commands in the following order from the project root.
$ php app/console assets:install
$ php app/console assetic:dump
$ php app/console cache:clear

Sonata Media Bundle - How to limit file extension on specific Context?

I created a context that would contain only zip files. I need to be able to only allow zip file extension on this context. I was able to create a custom provider that extends to FileProvider but having a problem setting the specific extension allowed on this provider.
I followed this post: sonata-media-bundle-how-to-write-custom-provider
When I set the configuration to the following:
providers:
custom:
allowed_extensions: ['zip']
allowed_mime_types: ['application/zip','application/x-zip']
Symfony throws an error:
Fatal error: Uncaught exception 'Symfony\Component\Config\Definition\Exception\InvalidConfigurationException' with message 'Unrecognized options "custom" under "sonata_media.providers"'
Manage to find it after going through the media bundle code.
Allowed extensions and mime types are 6 and 7 arguments for a provider. Following is copy of my service.yml file for declaring a custom provider service.
services:
sonata.media.provider.custom:
class: Application\Sonata\MediaBundle\Provider\CustomProvider
tags:
- { name: sonata.media.provider }
arguments:
- sonata.media.provider.custom
- #sonata.media.filesystem.local
- #sonata.media.cdn.server
- #sonata.media.generator.default
- #sonata.media.thumbnail.format
- ['zip', 'foo']
- ['application/zip', 'foo/bar']

After tagging a service in services.yml symfony starts throwing ReflectionException

I have the following configuration in my SF2 Bundle
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
Which works like a charm.
Now I want to enhance the configured service and tag it so it gets the knp_paginator injected (I've also tried with other tags). My config becomes:
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }
For the mentioned tag to work the service implements the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware. As mentioned on the KnpPaginatorBundle docs.
Running my app will show the following:
ReflectionException: Class %catalogue.title.class% does not exist.
Since it works without adding the tags part, I know the class does exist.
Replacing %catalogue.title.class% with the value of the parameter (the fully qualified class name) solves the issue, but we are using this way of configuring our service classes in all our projects and really don't want to deviate from that just to resolve this issue.
[EDIT] Using a different parameter like catalogue_title_class or my_abc has the same result.
I'm running Symfony version 2.3.5.
What am I doing wrong?
Probably this exceptions throws because of same names of class of parameter and provided service name.
Try to rename
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
to some other name
parameters:
catalogue_title.class: My\SomeBundle\Services\TitleService
and use this
services:
catalogue.title:
class: %catalogue_title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }

Resources