13

I need to make payment transaction for an application and I saw JSMPaymentCoreBundle.

I read the documentation of JSMPaymentCoreBundel but I wonder me how I can pass object to the controller detailsAction(Order $order) and completeAction(Order $order).

For example, in the detailAction controller, the redirect response looks like this :

return new RedirectResponse($this->router->generate('payment_complete', array('orderNumber' => $order->getOrderNumber(),)));

For me, we don"t pass the required Order object in param to the completeAction controller below but only orderNumer:

/**
* @Route("/{orderNumber}/complete", name = "payment_complete")
*/
public function completeAction(Order $order){
    ...
}

I think that if I don't pass an Order object, I'll get error. So what is the best way to do that and how ?

New in development and Symfony, I really want to understand and not simply make a copy/paste.

Any help would be appreciate.

Benjamin Lucas
  • 370
  • 5
  • 20
  • This guy has got more than the bounty that he is offering. +1 because you keen on your problem more than your reputation. – Sami Eltamawy May 14 '15 at 07:10

2 Answers2

4

You can use @ParamConverter annotation to conver an orderNumber to its entity

http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
...
    /**
    * @Route("/{orderNumber}/complete", name = "payment_complete")
    * @ParamConverter("order", options={"mapping": {"orderNumber": "orderNumber"}})
    */
    public function completeAction(Order $order){
        ...
    }

Update: read your question again and little bit confused. So do you have a problem (error) to get $order or do you just confused why you pass OrderNumber, but getting Order entity?

If so,just ignore my first example that do same as shown:

/**
     * @Route("/{orderNumber}/details", name = "payment_details")
     * @Template
     */
    public function detailsAction(Order $order)

It means that ParameterConverter will do a magic for your to convert passed orderNumber to an entity Order, that actually is "best practice" approach recommended by Symfony doc: http://symfony.com/doc/2.3/best_practices/controllers.html#using-the-paramconverter

And you do not need to add an additional annotation for such case

Evgeniy Kuzmin
  • 2,384
  • 1
  • 19
  • 24
3

You think you will get error, but did you actually get an error?

The documentation you read for JSMPaymentCoreBundel is absolutely right. If you type hint the parameter in your controller action, the route placeholder will be converted to respective object, if found. See the link posted by Evgeniy. You dont even need to use ParamConverter

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\BlogBundle\Entity\Post;
/**
 * @Route("/blog/{id}")
 */
public function showAction(Post $post)
{
    //your code here
}

Several things happen under the hood:

The converter tries to get a SensioBlogBundle:Post object from the request attributes (request attributes comes from route placeholders -- here id);

If no Post object is found, a 404 Response is generated;

If a Post object is found, a new post request attribute is defined (accessible via $request->attributes->get('post'));

As for other request attributes, it is automatically injected in the controller when present in the method signature.

If you use type hinting as in the example above, you can even omit the @ParamConverter annotation altogether:

Broncha
  • 3,794
  • 1
  • 24
  • 34
  • Yes right, I added some explantation, cause also was confused about an error – Evgeniy Kuzmin May 07 '15 at 15:32
  • Ooooh I realy don't know that ! Powerful. But I always have a question but perhaps I had to make a new question for explanation. In the detailActions(), if I onderstood, I need to pass an orderNumber and the converter check if an object (in db) exists. In this case, I need to save the order in DB before redirection to detailsAction. If I always in the right way, why do we need to persist the order in the isValid condition ? Many thanks to all of you for this answers ! – Benjamin Lucas May 18 '15 at 09:02