T
T
TANK_IST2017-07-29 00:57:18
symfony
TANK_IST, 2017-07-29 00:57:18

How to initialize symfony routing without using symfony?

I decided to use the symfony routing component on my project.
I created a routing.yaml config, but now I don't know how to initialize it.
A google search turned up nothing.
Help. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TANK_IST, 2017-07-29
@TANK_IST

<?php

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel;

$locator = new FileLocator(array(__DIR__));
$loader = new YamlFileLoader($locator);
$routes = $loader->load('route.yml');

$context = new RequestContext();
$request = Request::createFromGlobals();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
$argumentResolver = new HttpKernel\Controller\ArgumentResolver();

try {
    $matcher = $matcher->match($request->getPathInfo());
    $matcher['_controller'] .= 'Action';
    $request->attributes->add($matcher);

    $controller = $controllerResolver->getController($request);
    $arguments = $argumentResolver->getArguments($request, $controller);

    call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
    $response = new Response('Not found!', Response::HTTP_NOT_FOUND);
}

P
padlyuck, 2017-07-29
@padlyuck

https://www.sitepoint.com/build-php-framework-symf... when not googled - ask a question in English. I don’t know how the quality is, but the manual chews up the process of using routing, evolving it from a simple switch-case to using a symphonic component

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question