Search code examples
methodsscopecontrollerpimcore-v5

Reverse construction of Custom Routes from inside a Controller using Pimcore 5.x


As per the Pimcore 5 Documentation:

URLs are generated using the default URL helper of Symfony $this->path() and $this->url(). Additionally to the standard helpers for generating URLs, Pimcore offers a special templating helper ($this->pimcoreUrl()) to generate URLs like you did with Pimcore 4. You can define a placeholder in the reverse pattern with %NAME and it is also possible to define an optional part, to do so just embrace the part with curly brackets { } (see example below).

https://pimcore.com/docs/5.0.x/Development_Documentation/MVC/Routing_and_URLs/Custom_Routes.html

I should be able to reverse construct a route using the path method like so:

$this->path( 'MyRouteName', [
  'route_param_a' => 'A',
  'route_param_b' => 'B',
  'route_param_c' => 'C'
] );

Unfortunately, when I call this from inside a Controller, I get the following error:

Attempted to call an undefined method named "path" of class "AppBundle\Controller\MyController".

Is there a similar function or method available in the Controller scope that I can use to generate my paths when I respond with my JSON object directly from the controller (without using a view)?


Solution

  • Looks like the answer for this is not covered in the Pimcore 5 documentation, rather the Symfony 3 documentation!

    https://symfony.com/doc/current/routing.html#generating-urls

    $url = $this->generateUrl( 'MyRouteName', [
      'route_param_a' => 'A',
      'route_param_b' => 'B',
      'route_param_c' => 'C'
    ] );