Created
December 5, 2014 15:41
-
-
Save cawa87/70423b5799652ee78d4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace VswSystem\CoreBundle\Controller\Listener; | |
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use VswSystem\CmsBundle\Controller\PageController; | |
/** | |
* Listen to /page/alias and redirect to controller action | |
* Class PageControllerListener | |
* @package VswSystem\CoreBundle\Controller | |
*/ | |
class PageControllerListener | |
{ | |
protected $resolver; | |
protected $aliasRepo; | |
function __construct(ControllerResolver $resolver, $aliasRepo) | |
{ | |
$this->resolver = $resolver; | |
$this->aliasRepo = $aliasRepo; | |
} | |
public function onKernelController(FilterControllerEvent $event) | |
{ | |
$controller = $event->getController(); | |
/* | |
* $controller passed can be either a class or a Closure. This is not usual in Symfony but it may happen. | |
* If it is a class, it comes in array format | |
*/ | |
if (!is_array($controller)) { | |
return; | |
} | |
if ($controller[0] instanceof PageController && $controller[1] == 'viewAction') { | |
$request = new Request(); | |
$alias = $this->aliasRepo->findOneBy(['alias' => $event->getRequest()->attributes->get('alias')]); | |
$action = ''; | |
if(!$alias || !$alias->getType()){ | |
throw new NotFoundHttpException('No such page!'); | |
} | |
switch ($alias->getType()) { | |
case 'TextContentPage': | |
$action = 'textPage'; | |
break; | |
case 'GalleryContentPage': | |
$action = 'galleryPage'; | |
break; | |
case 'FaqContentPage': | |
$action = 'faqPage'; | |
break; | |
case 'NewsContentPage': | |
$action = 'newsPage'; | |
break; | |
} | |
$request->attributes->set('_controller', 'VswSystemCmsBundle:Page:' . $action); | |
$event->setController($this->resolver->getController($request)); | |
// $event->setController('VswSystem\CmsBundle\Controller\GalleryController'); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment