<?php
namespace Factory\CartBundle\Classes\Traits\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
trait ControllerDispatchActionTrait
{
/**
* This function is used to call the right controller action since in static routes in Pimcore 10, you can't
* define action as a placeholder as controller and action are not entered as separate fields anymore
* @param Request $request
* @return Response
*/
public function dispatchAction(Request $request)
{
$action = $request->attributes->get('action');
$action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));
$actionMethod = lcfirst($action) . 'Action';
if (!method_exists($this, $actionMethod)) {
throw new NotFoundHttpException(sprintf('Action "%s" not found.', $actionMethod));
}
return $this->forward($this::class . '::' . $actionMethod, [
'request' => $request
]);
}
}