Bundle Does Not Exist: Symfony2 - symfony

I'm pretty new to Symfony2. I can't figure out what's going on. This code (set up to test if the bundle can be detected):
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ivory\GoogleMap\Overlays\Animation;
use Ivory\GoogleMap\Overlays\Marker;
class DefaultController extends Controller {
public function mapAction() {
$map = $this->get ( 'ivory_google_map.map' );
return $this->render ( 'KrewMediaLocalFarmBundle:Default:map.html.twig', array('map' => $map) );
}
}
works, rendering a simple map, while this code (the real code that involves embedding a controller to render a map with data)
<?php
// localfarm/src/KrewMedia/Bundle/LocalFarmBundle/Controller/DefaultController.php
namespace KrewMedia\Bundle\LocalFarmBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ivory\GoogleMap\Overlays\Animation;
use Ivory\GoogleMap\Overlays\Marker;
class DefaultController extends Controller {
public function mapAction() {
//$map = $this->get ( 'ivory_google_map.map' );
return $this->render ( 'KrewMediaLocalFarmBundle:Default:maptest.html.twig');
}
}
gives me this error: "An exception has been thrown during the rendering of a template ("Bundle "LocalFarmBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your AppKernel.php file?") in KrewMediaLocalFarmBundle:Default:maptest.html.twig at line 3."
Both maptest.html.twig and map.html.twig are in the same folder in the LocalFarmBundle. I wonder why the bundle is found in the first piece of code but not in the second. The relevant routing is this
krew_media_local_farm_homepage:
pattern: /index
defaults: { _controller: KrewMediaLocalFarmBundle:Default:index }
krew_media_local_farm_map:
pattern: /map
defaults: { _controller: KrewMediaLocalFarmBundle:Default:map }
krew_media_basic_map:
pattern: /map/basic
defaults: { _controller: KrewMediaLocalFarmBundle:Map:basic }
map.html.twig:
{{ google_map_container(map) }}
{{ google_map_js(map) }}
maptest.html.twig:
Map Test
{% render(controller( 'LocalFarmBundle:Map:basic')) %}
The controller for 'LocalFarmBundle:Map:basic':
public function basicAction() {
// set up map
$map = $this->get ( 'ivory_google_map.map' );
// Get User geo info
$user = $this->getUser ();
if (isset ( $user )) { // check to see if logged in: map is useless without it
$map->setAutoZoom ( true );
$map->setStylesheetOption('width', '500px');
$map->setStylesheetOption('height', '500px');
$radius = 1;
// get repository for user class
$coordList = $this->container->get ( 'sylius.repository.user' )->findUsersNearUser ( $user, $radius );
// set user marker
$this->placeMarker ( $user, "/assets/img/home.png", $map );
if (! empty ( $coordList )) {
foreach ( $coordList as $geo ) {
$this->placeMarker ( $geo, "/assets/img/neighbor.png", $map );
}
}
// get furthest distance
$dist = $this->getFurthestDistance ( $user, $coordList );
// set invisible boundary markers
$this->addBoundaries ( $user, $dist, $map );
// render the map
} else {
echo "You are not logged in. Please log in and try again.";
}
return $this->render ( 'KrewMediaLocalFarmBundle:Default:map.html.twig', array (
'map' => $map
) );
Any help would be appreciated in figuring out this problem.

render tag requires full name of bundle, so change
{% render(controller( 'LocalFarmBundle:Map:basic')) %}
to
{% render(controller( 'KrewMediaLocalFarmBundle:Map:basic')) %}

Related

In Symfony how do I redirect to route with datas from search form ? Error : Variable does not exist

I made a search form in order to get events list. This form is displayed in front/search.html.twig. When I submit the search form, I'd like it leads me to front/events.html.twig.
When I submitted it, it says "category" doesn't exist. I don't have this problem when I replaced
return $this->redirectToRoute('events', $data);
with
return $this->render('front/events.html.twig', $data);
but I wish to use to route "events"...
This is my EventsController.php file :
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request, SessionInterface $sessionInterface)
{
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
return $this->redirectToRoute('events', $data);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
#[Route('/events', name: 'events')]
public function events(
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository
){
$events = $eventsRepository->findAll();
$categories = $categoriesRepository->findAll();
return $this->render("front/events.html.twig", ['events' => $events, 'categories' => $categories]);
}
}
Bonjour Emilie,
Your route events does not have parameters. So you can't redirect it using parameters.
you can try something like this :
public function index($name)
{
return $this->redirectToRoute('events', ['max' => 10]);
}
You can forward to another Controller :
public function index($name)
{
$response = $this->forward('App\Controller\OtherController::fancy', [
'name' => $name,
'color' => 'green',
]);
// ... further modify the response or return it directly
return $response;
}
Regards,
hous has found the solution :
The second parameter must be an array :
return $this->redirectToRoute('events', [$data]);

Symfony 5 : handling multiple forms in one controller

I am trying to manage multiple forms in the same page in Symfony 5 with the following function, but it seems that every time I try to submit a form, only the first form of the list is handled even if it is not the one that has been submitted:
class ContentController extends AbstractController
{
/**
* #Route("/category/edition/{content}", name="edit_category")
*/
public function edition(Request $request, Category $content): Response
{
$forms = [
"edit_category" => $this->createForm(EditCategoryType::class, $content),
"create_post" => $this->createForm(CreatePostType::class)
];
foreach($forms as $form) {
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
// Always prints edit_category
// even when that is the the create_post that is submitted
return var_dump($form->getName());
}
}
return $this->render(
'content/edition.html.twig',
[
'forms' => \array_map(
function($form) {
return $form->createView();
},
$forms
),
'content' => $content,
]
);
}
}
I have seen in other posts that the name of the forms can sometime raise an issue, but I have checked that the forms do have different names, and I have also tried to call handleRequest() on every form in a separate foreach loop because I have seen this done in some posts, but it (quite expectedly I must say) didn't change the behavior.
And I didn't seem to find any unanimous best practice tips about how to handle multiple forms in the same Controller in Symfony, so I was wondering what is the best way to do it, or if it would be cleaner to define a separate action route for each form in order to avoid this problem altogether.
If needed, the content/edition.html.twig file looks something like that:
{% set edit_category_form = forms['edit_category'] %}
{% set create_post_form = forms['create_post'] %}
{{ form_start(edit_category_form) }}
{{ form_errors(edit_category_form) }}
{{ form_end(edit_category_form) }}
{{ form_start(create_post_form) }}
{{ form_errors(create_post_form) }}
{{ form_end(create_post_form) }}
(Category is a classical Symfony entity, EditCategoryType is a form associated with the Category entity and CreatePostType is a form associated with another Symfony entity)
After some research, it seems for some reason like it works if (and only if?) the form is build just before handling the request:
class ContentController extends AbstractController
{
/**
* #Route("/category/edition/{content}", name="edit_category")
*/
public function edition(Request $request, Category $content): Response
{
$self = $this;
$formBuilders = [
"edit_category" => function() use ($self, $content) {
return $self->createForm(EditCategoryType::class, $content);
},
"create_post" => function() use ($self, $content) {
return $self->createForm(CreatePostType::class);
},
];
$forms = [];
foreach($formBuilders as $key => $formBuilder) {
$form = $formBuilder();
$forms[$key] = $form;
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
// Does print the name of the right form
return var_dump($form->getName());
}
}
return $this->render(
'content/edition.html.twig',
[
'forms' => \array_map(
function($form) {
return $form->createView();
},
$forms
),
'content' => $content,
]
);
}
}
It works but it doesn't feel like a proper way to handle this !

Create a custom route in a custom page

I'm using Symfony 4.3 and Sonata 3.x version.
I'm trying to create a custom route in a custom Page but I get the error :
An exception has been thrown during the rendering of a template ("unable to find the route `admin.photocall|admin.photocall_gallery.moderate`")
I have an entity X with a OneToMany relation to the Y entity.
Explanation with code :
class XAdmin extends AbstractAdmin
{
[...]
protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
$admin = $this->isChild() ? $this->getParent() : $this;
$id = $admin->getRequest()->get('id');
if ($this->isGranted('LIST')) {
$menu->addChild('Galerie', [
'uri' => $admin->generateUrl('admin.photocall_gallery.list', ['id' => $id])
]);
}
}
}
Then there is my YAdmin :
class YAdmin extends AbstractAdmin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('_action', null, [
'actions' => [
'clone' => [
'template' => 'admin/PhotocallListAdmin/__list_action_moderate.html.twig'
]
]
])
;
}
protected function configureRoutes(RouteCollection $collection)
{
if ($this->isChild()) {
$collection->clearExcept(['list', 'moderate']);
$collection->add($collection->getBaseCodeRoute().'.moderate', 'moderate');
return;
}
}
}
So there, I add an action with a template which look like this :
<a class="btn btn-sm" href="{{ admin.generateObjectUrl('moderate', object) }}">
{% if not object.ismoderate %}
Moderate
{% else %}
Undo moderation
{% endif%}
</a>
So the error says that it's unable to find the route admin.photocall|admin.photocall_gallery.moderate. But when I dump the $collection in YAdmin after adding the moderate route, I have two elements :
admin.photocall|admin.photocall_gallery.list (the current page)
admin.photocall|admin.photocall_gallery.moderate
I searched but it looks like that nobody else did this.
Thanks for you help
I write Gaska's comment to close this issue.
I just had to add :
$collection->add('moderate', 'moderate'); and then clear the cache.
Thanks to him

How to recover data from database using symfony2

i need to display information of a product from my database but i have an
error: Controller
"UserBundle\Controller\DefaultController::profileAction()" requires
that you provide a value for the "$id" argument (because there is no
default value or because there is a non optional argument after this
one).
This is the profileAction:
public function profileAction($id)
{
$product = $this->getDoctrine()
->getRepository('UserBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
return $this->render('UserBundle:Default:profile.html.twig', array('product' =>$product));
}
please someone help me.
You need to provide $id as argument.
Change the return to something like that:
return $this->render('UserBundle:Default:profile.html.twig', ['product' => $product, 'id' => $id]);
You need a route like that :
_my_route:
path: /my_path/{id}
defaults: {controller: AcmeBundle:Default:profile}
requirements:
id: \d+
Action in your controller :
public function profileAction($id)
{
$product = $this->getDoctrine()
->getRepository('UserBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
return $this->render('UserBundle:Default:profile.html.twig', array('product' =>$product));
And a link or something else in your view like that (Hope you use Twig):
Lorem ipsum
It should be work.
EDIT :
For you search feature, i will do a route like that
_profile:
path: /profile
defaults: {_controller:AcmeBundle:Default:profile}
Controller :
public function profileAction(Request $request){
$form = $this->createForm(new SearchType());
$products = null ;
if($request->isMethod('POST')){
$form->handleRequest($request);
$value = ($form['search']->getData());// change de 'search' according to your form
$em = $this->getDoctrine()->getManager() ;
$product = $em->getRepository('UserBundle:Product')->find($value) ;
}
if(!$product) throw $this->createNotFoundException('your message');
return $this->render('UserBundle:Default:profile.html.twig',array(
'product' => $product
));
}
And finally, your form in the view :
<form method="POST" action="{{ path('_profile') }}">
//your code
</form>
Well i think that here $id become null and it causes error because $id is used to fetch data from your db, so set default value for $id check $id will never become null or empty. All the best.

Default to blank string in Twig template if translation is not found

Is there a way to default to a blank string rather than the translation key in the event the translation has not been found in a Twig template?
I am attempting this sort of thing using the default Twig filter alongside the trans filter however this does not work:
{{ 'crmpicco.general.course.opening_hours_weekend'|default('')|trans }}
You can overwrite the translation extension with your own, so the trans and transchoice filter would behave as you want:
<?php
// src/AppBundle/Twig/EmptyTranslationExtension.php
namespace AppBundle\Twig;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
class EmptyTranslationExtension extends TranslationExtension
{
public function trans($message, array $arguments = [], $domain = null, $locale = null)
{
$value = parent::trans($message, $arguments, $domain, $locale);
return ($message === $value) ? '' : $value;
}
public function transchoice($message, $count, array $arguments = [], $domain = null, $locale = null)
{
$value = parent::transchoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
return ($message === $value) ? '' : $value;
}
}
And register your extension as replacement for the default one:
# app/config/services.yml
services:
twig.extension.trans:
class: AppBundle\Twig\EmptyTranslationExtension
public: false
arguments:
- #translator
tags:
- { name: twig.extension }
This way work, but it is not an optimal solution:
{% set trans_key = 'crmpicco.general.course.opening_hours_weekend' %}
{% set trans_value = trans_key | trans %}
{{ trans_key == trans_value ? '' : trans_value }}
This part works for me:
{{ crmpicco.general.course.opening_hours_weekend == '' ? '' : crmpicco.general.course.opening_hours_weekend|trans() }}
it's even easier than that. you can simply cast the type to string by concatenating with a string.
{{ null ~ ''|trans }}
but given the choice, clean data would be better. or a fix in the translationextension

Resources