Router example

public function onDispatchLoopStartup(Enlight_Controller_EventArgs $args)
    {
        $this->request = $args->getSubject()->Request();
        $this->response = $args->getSubject()->Response();

        if ($this->request->getModuleName() != 'api') {
            return;
        }

        $this->isApiCall = true;

        $router = new Router();
        $router->assembleRoute($this->request, $this->response);
    }

    /** * This pre-dispatch event-hook checks permissions * * @return void */
    public function onFrontPreDispatch(Enlight_Controller_EventArgs $args)
    {
        $request = $args->getRequest();
        
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class RouterTest extends TestCase
{
    public function testConstructThrowsOnNonSymfonyNorPsr11Container()
    {
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('You should either pass a "Symfony\Component\DependencyInjection\ContainerInterface" instance or provide the $parameters argument of the "Symfony\Bundle\FrameworkBundle\Routing\Router::__construct" method');
        new Router($this->createMock(ContainerInterface::class), 'foo');
    }

    public function testGenerateWithServiceParam()
    {
        $routes = new RouteCollection();

        $routes->add('foo', new Route(
            ' /{_locale}',
            [
                '_locale' => '%locale%',
            ],
            [
$route_collection->add('user_view', $route);

    $route = new Route('/user/login');
    $route->setOption('compiler_class', RouteCompiler::class);
    $route_collection->add('user_login', $route);

    $route_provider->getRouteCollectionForRequest(Argument::any())
      ->willReturn($route_collection);

    $url_generator = $this->prophesize(UrlGeneratorInterface::class);
    $current_path_stack = $this->prophesize(CurrentPathStack::class);
    $router = new Router($route_provider->reveal()$current_path_stack->reveal()$url_generator->reveal());

    $request_context = $this->createMock(RequestContext::class);
    $request_context->expects($this->any())
      ->method('getScheme')
      ->willReturn('http');
    $router->setContext($request_context);

    $current_path_stack->getPath(Argument::any())->willReturn('/user/1');
    $result = $router->match('/user/1');

    $this->assertEquals('user_view', $result['_route']);

    
if ($request->getParam('sAction') && $request->getParam('sAction') === 'doSale') {
                    $request->setControllerName('checkout')->setActionName('finish')->setDispatched(false);
                } else {
                    $request->setControllerName('checkout')->setActionName('confirm')->setDispatched(false);
                }
                break;
            case 'cat':
                $request->setControllerName('listing')->setDispatched(false);
                break;
            case 'supplier':
                $url = $args->getSubject()->Router()->assemble([
                        'controller' => 'listing',
                        'action' => 'manufacturer',
                        'sSupplier' => $request->getParam('sSupplier'),
                ]);
                $args->getSubject()->Response()->setRedirect($url, 301);
                break;
            case 'captcha':
                $request->setModuleName('widgets')->setControllerName('captcha')->setDispatched(false);
                break;
            case 'password':
            case 'login':
            
use Symfony\Component\Routing\Router;

class RouterTest extends TestCase
{
    private Router $router;
    private MockObject&LoaderInterface $loader;
    private string $cacheDir;

    protected function setUp(): void
    {
        $this->loader = $this->createMock(LoaderInterface::class);
        $this->router = new Router($this->loader, 'routing.yml');

        $this->cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true);
    }

    protected function tearDown(): void
    {
        if (is_dir($this->cacheDir)) {
            array_map('unlink', glob($this->cacheDir.\DIRECTORY_SEPARATOR.'*'));
            @rmdir($this->cacheDir);
        }
    }

    

  public function testGenerateUnsupported() {
    $this->expectException(\BadMethodCallException::class);
    $route_provider = $this->prophesize(RouteProviderInterface::class);
    $current_path_stack = $this->prophesize(CurrentPathStack::class);
    $url_generator = $this->prophesize(UrlGeneratorInterface::class);
    $route_name = 'test.route';
    $route_path = '/test';
    $url_generator
      ->generate($route_name, Argument::any(), Argument::any())
      ->willReturn($route_path);
    $router = new Router($route_provider->reveal()$current_path_stack->reveal()$url_generator->reveal());
    $router->generate($route_name);
  }

  /** * @covers ::addDeprecatedRouteFilter * @covers ::addDeprecatedRouteEnhancer */
  public function testDeprecatedAdd() {
    // Test needs access to router's protected properties.     $filters = new \ReflectionProperty(Router::class, 'filters');
    $enhancers = new \ReflectionProperty(Router::class, 'enhancers');

    
return $finder->find($uri);
    }

    private function createRouter(Request $request): Router
    {
        $routes = Services::routes();

        if ($this->resetRoutes) {
            $routes->resetRoutes();
        }

        return new Router($routes$request);
    }

    private function createFilters(Request $request): Filters
    {
        $config = config(FiltersConfig::class);

        return new Filters($config$request, Services::response());
    }
}

    public static function router(?RouteCollectionInterface $routes = null, ?Request $request = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('router', $routes$request);
        }

        $routes ??= AppServices::routes();
        $request ??= AppServices::request();

        return new Router($routes$request);
    }

    /** * The Security class provides a few handy tools for keeping the site * secure, most notably the CSRF protection tools. * * @return Security */
    public static function security(?SecurityConfig $config = null, bool $getShared = true)
    {
        if ($getShared) {
            
Home | Imprint | This part of the site doesn't use cookies.