Answer the question
In order to leave comments, you need to log in
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
<?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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question