symfony2 routing parameters and request - symfony

I have this route
edit_project:
pattern: /edit/{id}
defaults: { _controller: CpjProjectsBundle:Project:edit }
requirements:
id: \d+
and this is the controller:
public function editAction(Request $request)
{
}
inside the controller I'm unable to receive id
$this->query->get('id'); //empty
if I change the method signature, it works:
public function editAction($id)
but I need the Request to handle the form, usualy in this way
$form->handleRequest($request);
any suggestion for a workaround?
many thanks

Your final URL looks like this: http://my.domain.com/edit/123 so there is no query part of URL (...?id=123). You should accept both the Request object and id as arguments of your function:
public function editAction(Request $request, $id)
{
var_dump($request, $id);
}

Using that url if you want access to the id you will pass it as another parameter to your function. But it seems like you may want to get the Project entity associated with that id. You can save a step in your controller and have symfony look it up for you by type hinting the $id variable like so:
<?php
namespace Cpj\ProjectsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Cpj\ProjectsBundle\Entity\Project;
//Other use statements
class PrjectController extends Controller
{
public function editAction(Request $request, Project $id)
{
var_dump($id);
// This will be and instance of Cpj\ProjectsBundle\Entity\Project
//if you need the actual ID of it you can do the following
$realID = $id->getId()
}
}

Related

Hot to read text from URL in symfony2?

i've a url like this:
http://local.gogutsi.com/backend/quote/workticket/27119/driver/197?_format=pdf&email_to_driver=1&ret=/backend/quote/workticket/27119/driver/197%3Fret%3D/backend/quote/quotes%253Fquotes_list%255Bstart%255D%253D%2526quotes_list%255Bend%255D%253D
i want to read _format value from it.
Into controller that rule that url
use Symfony\Component\HttpFoundation\Request;
//class declaration here ...
public function yourControllerNameHere(Request $request)
{
//...
$format = $request->query->get('_format');
//...
}

symfony2: trying to pass parameters on the URL

I have this routing.yml inside a bundle:
project_backend_update_item:
path: /update-item/{id}
defaults: { _controller: ProjectBackendBundle:Default:updateItem }
and this inside my controller:
public function updateItemAction(Request $request)
{
$repository = $this->getDoctrine()
->getRepository('ProjectFrontendBundle:Item');
var_dump($request->query->get('id'));
And when I request: "app_dev.php/update-item/1" I get NULL. Why? I expected "1".
$request->query give you the $_GET parameters (for example with /update-item?id=5)
Your parameter 'id' is not passed with _GET, but with routing.
You must do :
public function updateItemAction($id)
{
var_dump($id);
}
Or
public function updateItemAction(Request $request, $id)
{
var_dump($id);
}
In Symfony, whenever you have a parameter in your route pattern, you may add a variable with the same name in your controller action.
In your case, you should do something like this :
public function updateItemAction($id, Request $request)
{
$repository = $this->getDoctrine()
->getRepository('ProjectFrontendBundle:Item');
var_dump($id);
}

FOSRestBundle: The controller must return a response (Array()) given

I'm starting with FOSRestBundle. I have added this routing configuration:
//Sermovi/Bundle/APIBundle/Resources/config/routing.yml
sermovi_api_homepage:
type: rest
resource: Sermovi\Bundle\APIBundle\Controller\DefaultController
//app/config/routing.yml
sermovi_api:
type: rest
prefix: /api
resource: "#SermoviAPIBundle/Resources/config/routing.yml"
And this config.yml
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: true
sensio_framework_extra:
view:
annotations: false
And this controller:
namespace Sermovi\Bundle\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends FOSRestController
{
public function getArticlesAction()
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);
return array(
'entity' => $entity
);
}
}
And I'm getting this error:
[{"message":"The controller must return a response (Array(entity =>
Object(Sermovi\Bundle\ManagementBundle\Entity\Transaction))
given).","class":"LogicException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2855,"args":[]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handleRaw","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2817,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2946,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel\DependencyInjection","short_class":"ContainerAwareHttpKernel","class":"Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2247,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"Kernel","class":"Symfony\Component\HttpKernel\Kernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/web/app_dev.php","line":28,"args":[["object","Symfony\Component\HttpFoundation\Request"]]}]}]
EDIT:
"Could" I do something like this below? Or since FOSRestBundle is using JMSSerializerBundle I should not do it?
$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');
return new Response($serializedEntity);
There are several ways to setup your Controllers with FOSRestBundle. The way you are doing it, you must return a view. Here is a link to a rest controller on github that may help you out a bit. LiipHelloBundle has an example.
Also, I found it easiest to use the ClassResourceInterface in my controllers. This way, I return an array, and it handles all the serialization itself. It also uses your Controller name to generate the routes that are necessary, so I don't have to manually define any routes. It is my preferred way of setting up the Controller. See the doc entry here for how that works.
If you do end up using the ClassResourceInterface, be sure to include the following annotation for each action, it will make it so your returned array is serialized properly:
use FOS\RestBundle\Controller\Annotations as Rest;
//.....
/**
* #Rest\View()
*/
public function cgetAction() {}
You might even be able to do that with the way you are setting up the controller, but I haven't tried that before. Let us know if you go that way and it works.
UPDATE
For those who may be interested in using the FOSRestBundle without using the ClassResourceInterface, the problem with the controller action in the question is that it does not return a view. This should work in the action:
class DefaultController extends FOSRestController
{
public function getArticlesAction()
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);
$statusCode = 200;
$view = $this->view($entity, $statusCode);
return $this->handleView($view);
}
}

Symfony2 twig extension get caller template

Does anyone know a way to get the template (or its name) that called the Twig extension?
You could pass the referrer into the twig template...
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
return $this->render('DefaultBundle:Default:index.html.twig', array('referrer', $request->getRequestUri()));
}
}

Symfony 2.0.3 Global template variable

I've got an Entity that I want to associate with the users session.
I created a service so that I could reach this info from where ever.
in the service i save the entities id in an session variable
and in the getEntity() method i get the session variable and with doctrine find the entity and return it.
this way to the template i should be able to call {{ myservice.myentity.myproperty }}
The problem is that myservice is used all over the place, and I don't want to have to get it in every since Action and append it to the view array.
Is there a way to make a service accessible from all views like the session {{ app.session }} ?
The solution
By creating a custom service i can get to that from where ever by using
$this->get('myservice');
this is all done by http://symfony.com/doc/current/book/service_container.html
But I'll give you some demo code.
The Service
This first snippet is the actual service
<?php
namespace MyBundle\AppBundle\Extensions;
use Symfony\Component\HttpFoundation\Session;
use Doctrine\ORM\EntityManager;
use MyBundle\AppBundle\Entity\Patient;
class AppState
{
protected $session;
protected $em;
function __construct(Session $session, EntityManager $em)
{
$this->session = $session;
$this->em = $em;
}
public function getPatient()
{
$id = $this->session->get('patient');
return isset($id) ? $em->getRepository('MyBundleStoreBundle:Patient')->find($id) : null;
}
}
Register it in you config.yml with something like this
services:
appstate:
class: MyBundle\AppBundle\Extensions\AppState
arguments: [#session, #doctrine.orm.entity_manager]
Now we can as I said before, get the service in our controllers with
$this->get('myservice');
But since this is a global service I didn't want to have to do this in every controller and every action
public function myAction()
{
$appstate = $this->get('appstate');
return array(
'appstate' => $appstate
);
}
so now we go create a Twig_Extension
Twig Extension
<?php
namespace MyBundle\AppBundle\Extensions;
use MyBundle\AppBundle\Extensions\AppState;
class AppStateExtension extends \Twig_Extension
{
protected $appState;
function __construct(AppState $appState) {
$this->appState = $appState;
}
public function getGlobals() {
return array(
'appstate' => $this->appState
);
}
public function getName()
{
return 'appstate';
}
}
By using dependency injection we now have the AppState Service that we created in the twig extension named appstate
Now we register that with the symfony (again inside the services section inside the config-file)
twig.extension.appstate:
class: MyBundle\AppBundle\Extensions\AppStateExtension
arguments: [#appstate]
tags:
- { name: twig.extension }
The important part being the "tags", since this is what symfony uses to find all twig extensions
We are now set to use our appstate in our twig templates by the variable name
{{ appstate.patient }}
or
{{ appstate.getPatient() }}
Awesome!
Maybe you can try this in your action ? : $this->container->get('templating')->addGlobal($name, $value)

Resources