Symfony2 KnpMenuBundle - Following tutorial and came across this error - symfony

I followed this tutorial:
https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md#installation
And have come across the following error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "page_show" as such route does not exist.") in /var/www/bundles/src/Acme/DemoBundle/Resources/views/Default/index.html.twig at line 4.
Is there a step I am missing here to pass something to a controller?
From link:
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('About Me', array(
'route' => 'page_show',
'routeParameters' => array('id' => 42)
));
// ... add more children
return $menu;
}
}
To actually render the menu, just do the following from anywhere in any Twig template:
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu') }}

Do a ./app/console router:debug - it will show you all the routes registered in your application. I am guessing page_show is not one of them.
The documentation you are using probably expects you to add your own routes/pages to the menu like this:
$menu->addChild('Home', array('route' => 'homepage'));
Where 'homepage' has to already exist. So does 'show_page'. So you need a controller somewhere that handles a request to the show_page route, or exchange show_page for a route that you have already defined in your app. Hope I made sense.

Following the tutorial exactly, this error is caused by line 25 in the file
2 // src/Acme/MainBundle/Menu/MenuBuilder.php
...
25 $menu->addChild('Home', array('route' => 'homepage'));
The tutorial code assumes that you have a route called 'homepage'. Assuming you set this up inside a custom Bundle, then a quick way to solve this problem so you can get the tutorial up and running is to go to...
// src/Acme/MainBundle/Resources/config/routing.yml
...and copy the homepage route from there (will look something like acme_main_bundle_homepage)

Related

Knp menu translations

According to knp, this should be how you can easily add translations to your menu. It ain't working for me.
My menu class:
public function createMainMenuLeft(array $options)
{
$menuLeft = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'left')));
$menuLeft->addChild('test', array('route' => 'test_route'))
->setExtra('translation_domain', 'AppBundle');
return $menuLeft;
}
In AppBundle/Resources/translations/messages.en.yml I have
test: nothing
However, my menu still has the label 'test' not the value from the translations file. Am I missing something here?
My locale is set in config.yml to en.
After some research I found the way to do it inside the documentation from the bundle, not the one from symfony. The template hast to be overwritten. Source

How can I get custom routes inside sonata admin class

I configure knp menu inside sonata admin product class and I want to add link to another admin class (category)
my code is:
protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
$menu->addChild(
$this->trans('product.sidemenu.link_designer', array(), 'm2m'),
array('uri' => $admin->generateUrl('sonata.classification.admin.category.list'))
);
}
Unfortunatly route 'sonata.classification.admin.category.show' doesn't exist. Routes from app/console router debug also desnt work. I have no access to inject #router in servies becouse definition of service is inside vendors.
Any idea?
In your admin, you can override the configureRoutes method, as demonstrated in the documentation:
http://sonata-project.org/bundles/admin/2-2/doc/reference/routing.html#create-a-route
This will allow you to add custom routes for your admin.
I found simple solution. I was mistake because default menu item defined in sonata demo looks like this:
$menu->addChild(
$this->trans('product.sidemenu.view_variations'),
array('uri' => $admin->generateUrl('sonata.product.admin.product.variation.list', array('id' => $id)))
);
And to add custom routes I had to added route parameter instead uri.
$menu->addChild(
$this->trans('product.sidemenu.view_variations'),
array('route' => 'admin_sonata_classification_category_list' )
);
Now everything work .

Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig"

Error showed :
Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig" in SonataAdminBundle:CRUD:base_list_field.html.twig at line 23.
Configure entity for only show list(remove create, edit and delete routes) and showed this error, i find this template in sonata admin bundle but no exists, please help me with this issue.
There is really no template called: SonataAdminBundle:CRUD:list__action_show.html.twig
Your problem is not in routes, but in declaring wrong inline action in configureListFields method. The inline action should be called view so the SonataAdminBundle:CRUD:list__action_view.html.twig will be called.
The inline actions should be declared like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
//... some other fields ...
// add "show" link in each row of table
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
)
))
;
}

KnpMenuBundle and the proposed tutorial example: is something wrong?

The KNP MenuBundle is a Symfony2 bundle for handling menus in a very dynamic way. The bundle comes out with a simple tutorial example, provided here.
In the proposed example, within the Builder class, the authors supposed that a function setCurrentUri() has to be called on the $menu object. However, $menu is an instance of MenuItem class, which does not implements the above mentioned function.
To make the answer self contained, I report the code of the example class provided here:
<?php
// src/Acme/DemoBundle/Menu/Builder.php
namespace Acme\DemoBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->setCurrentUri($this->container->get('request')->getRequestUri());
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('About Me', array(
'route' => 'page_show',
'routeParameters' => array('id' => 42)
));
// ... add more children
return $menu;
}
}
PS: Notice that an important import is missing in this example, which I report in the following for sake of completeness:
use Symfony\Component\HttpFoundation\Request;
I am also looking for the documentation update, but as a temporary solution you can set versions in deps like these:
[KnpMenu]
git=https://github.com/KnpLabs/KnpMenu.git
version=v1.1.2
[KnpMenuBundle]
git=https://github.com/KnpLabs/KnpMenuBundle.git
target=/bundles/Knp/Bundle/MenuBundle
version=v1.1.0

How do I access the Twig path() function from a Controller?

Okay, I know I can't literally call a twig template function from a controller, but to make links, I usually do the {{ path('_routeName') }} and that's great.
However, now I want to formulate some links in the controller that will then be passed to the template via parameters like this:
$params = array(
'breadcrumbs' = array(
'Donuts' => '/donuts',
'Bearclaws' => '/donuts/bearclaws',
'Strawberry bearclaw' => null,
),
);
return $this->render('Bundle:Donut:info.html.twig', $params);
Except I don't want to hard-code those links. What I'd like is to be able to do
'Donuts' => path('_donutRoute'),
but how to reach the path method or equivalent?
If your controller is extending the Symfony2 Controller (Symfony\Bundle\FrameworkBundle\Controller\Controller) you can use the following to generate urls like this :
$this->generateUrl('_donutRoute')
If you want it with parameters use the following:
$this->generateUrl('_donutRoute', array('param1'=>'val1', 'param2'=>'val2'))
I found an alternative way to do this that I feel is equal to the one proposed by #d.syph.3r
The plan is to do:
'breadcrumbs' = array(
'Donuts' => 'donutsRoute',
'Bearclaws' => 'bearclawRoute',
'Strawberry bearclaw' => null,
)
Then in the twig template, do:
{% for name, route in breadcrumbs %}
{{ path(route) }}
The advantage here is that the Controller is not generating any HTML in this case.

Resources