1
votes

I have a Symfony app that uses Sonata as the backend admin and the Sylius platform for product management.

I have created my own create route in the admin class configureRoutes method. I want to call our resource controller (that will be used to handle all product objects) CRUD methods from sonata.

Here is my configureRoutes method thus far:

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('create', 'create', array(
                '_controller' => 'AppBundle:Backend/Resource:createAction',
                '_sylius' => array(
                    'template' => 'AppBundle:Product/Backend/Form:CreateProduct.html.twig',
                    'redirect' => 'sylius_backend_product_show',
                )
            )
        );
    }

But I get the following error:

enter image description here

My resourceController extends the sylius resource controller:

namespace AppBundle\Controller\Backend;

use Sylius\Bundle\ResourceBundle\Controller\ResourceController as BaseController;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class ResourceController
 * @package AppBundle\Controller\Backend
 */
class ResourceController extends BaseController
{
    /**
     * @param Request $request
     *
     * @return RedirectResponse|Response
     */
    public function createAction(Request $request)
    {   
        // Custom view logic
        return parent::createAction($request);
    }
}

My app/config.yml:

sylius_product:
    driver: doctrine/orm
    classes:
        product:
            model: AppBundle\Entity\Product
            controller: AppBundle\Controller\Backend\ResourceController
            form:
                default: AppBundle\Form\Type\ProductType
            translatable:
                targetEntity: AppBundle\Entity\ProductTranslation
        product_translation:
            model: AppBundle\Entity\ProductTranslation

sylius_resource:
    resources:
         app.backend.product:
             driver: doctrine/orm
             object_manager: default
             classes:
                 controller: AppBundle\Controller\Backend\ResourceController
                 model: AppBundle\Entity\Product

How do I satisfy the constructor arguments?

1
Sylius controllers are services so should be called in the sylius.controller.product:createAction format rather than Sylius:Product:create.qooplmao
Add that as an answer my man :)Kal
Out of curiosity, from what you can see, can you do the same thing but for resource controller so you can call it by sylius.controller.resource.Kal
I'm not sure I follow you.qooplmao

1 Answers

1
votes

Sylius controllers are services so should be called in the sylius.controller.product:createAction format rather than Sylius:Product:create.

The resource bundle names the services based on your resource name (app.backend.product) but the naming is generated like...

list($prefix, $resourceName) = explode('.', $name);

...meaning that your prefix would be app and the resource name would become backend (products not even being included in the naming). As the resource controllers actions output are all controlled by the routing information there isn't really the need to have a specific backend controller. If any actions were needed to be just for backend routes you could always create createBackendAction although, as I mentioned, it's probably not necessary.