vendor/kobizo/core-bundle/src/Controller/KobizoFeAbstractController.php line 46

Open in your IDE?
  1. <?php
  2. namespace Kobizo\Bundle\CoreBundle\Controller;
  3. use Kobizo\Component\Configuration\Backend\AdminUrlConfig;
  4. use Kobizo\Component\Configuration\Frontend\TemplateConfig;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. abstract class KobizoFeAbstractController extends AbstractController
  9. {
  10.     protected AdminUrlConfig $adminUrlConfig;
  11.     protected TemplateConfig $templateConfig;
  12.     public function __construct(
  13.         AdminUrlConfig $adminUrlConfig,
  14.         TemplateConfig $templateConfig
  15.     ) {
  16.         $this->adminUrlConfig $adminUrlConfig;
  17.         $this->templateConfig $templateConfig;
  18.     }
  19.     /**
  20.      * Merge {admin} slug, which is needed for path generation, before redirecting.
  21.      */
  22.     protected function redirectToRoute(string $route, array $parameters = [], int $status 302bool $inclDefaultParams false): RedirectResponse
  23.     {
  24.         if ($inclDefaultParams) {
  25.             $parameters array_merge($this->getDefaultParameters(), $parameters);
  26.         }
  27.         return parent::redirectToRoute($route$parameters$status);
  28.     }
  29.     /**
  30.      * Renders a view regarding to its existence in App and in current Bundle, where the function is called.
  31.      * The view of the App will have a higher priority against the view of Bundle.
  32.      *
  33.      * For example:
  34.      * In DefaultController.php, when $this->render('@KobizoCore/frontend/default/index.twig', [..]) is called
  35.      * This function will replace '@KobizoCore/frontend' with 'template_name' (eg. `asia-ho`)
  36.      * then will check if `App/templates/`asia-ho`/default/index.twig` exists to use it
  37.      * Otherwise the template of current bundle `@KobizoCore/frontend/default/index.twig will be used.
  38.      */
  39.     protected function render(string $view, array $parameters = [], Response $response null): Response
  40.     {
  41.         $parameters array_merge($this->getDefaultParameters(), $parameters);
  42.         //.twig file in APP if exists...
  43.         $appView str_replace('@KobizoCore/frontend'$this->templateConfig->getValue(nulltrue), $view);
  44.         //... then render this file from APP template folder
  45.         if ($this->get('twig')->getLoader()->exists($appView)) {
  46.             return parent::render($appView$parameters$response);
  47.         }
  48.         //if not then render original template file from this ecommerce bundle
  49.         return parent::render($view$parameters$response);
  50.     }
  51.     private function getDefaultParameters()
  52.     {
  53.         return [
  54.             'admin' => $this->adminUrlConfig->getValue(),
  55.         ];
  56.     }
  57. }