How can I pass Order object to detailsAction and CompleteAction using JMS payementCoreBundle

223 views Asked by At

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.

2

There are 2 answers

2
Broncha On BEST ANSWER

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:

0
Evgeniy Kuzmin On

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