Skip to content

Instantly share code, notes, and snippets.

@lfcipriani
Created October 27, 2016 11:46
Show Gist options
  • Save lfcipriani/1514a3138b8a7e718b5b0c5eac9259c5 to your computer and use it in GitHub Desktop.
Save lfcipriani/1514a3138b8a7e718b5b0c5eac9259c5 to your computer and use it in GitHub Desktop.
<?php
namespace Frontend;
use Common\Cache\CacheLayer;
use Common\City\Entity\City;
use Common\City\Service\CityService;
use Common\CommonRoutes;
use Common\Mvc\Application;
use Fabfuel\Prophiler\Plugin\Manager\Phalcon;
use Frontend\Controller\IndexController;
class FrontendRoutes extends CommonRoutes
{
const MODULE_NAME = Application::MODULE_FRONTEND;
const CATCH_ALL = 'frontend-catch-all';
const FRONTEND_CITYPAGE = 'frontend-city-page';
protected function initializeRoutes()
{
$this->add('(.*)', ['controller' => 'catch_all', 'action' => 'index', 'key' => 1])
->setName(static::CATCH_ALL);
$this->add('/{key:[A-Za-züÜ\.\-]+}{extra:/?(.*)?}', ['controller' => 'city', 'action' => 'index'])
->beforeMatch(function($uri, $route, $router){
$di = \Phalcon\Di::getDefault();
preg_match($route->getCompiledPattern(), $uri, $matches);
if (!isset($matches[1])) {
return false;
}
$cityCode = $matches[1];
/** @var CityService $cityService */
$cityService = $di->get('city');
$city = $cityService->findByUrlPart($cityCode);
return $city !== false;
})
->setName(static::FRONTEND_CITYPAGE);
/** This initialization should stay after the CMS route */
parent::initializeRoutes();
}
protected static function getNamespace()
{
return IndexController::getNamespace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment