Created
September 18, 2021 02:38
-
-
Save dmouse/f042bc4e5e8245bb25473db8cdb00e67 to your computer and use it in GitHub Desktop.
How to integrate my own route system with Symfony components. (Router)
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 Legacy; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | |
use Symfony\Component\Config\FileLocator; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver; | |
use Symfony\Component\HttpKernel\Controller\ArgumentResolver; | |
use Symfony\Component\Routing\RequestContext; | |
use Symfony\Component\Routing\Matcher\UrlMatcher; | |
class App | |
{ | |
private $container; | |
public function resolveRoute(): void | |
{ | |
$controllerResolver = $this->getContainer()->get(ContainerControllerResolver::class); | |
$argumentResolver = $this->getContainer()->get(ArgumentResolver::class); | |
$params = $this->getContainer()->get(UrlMatcher::class) | |
->match($this->getContainer()->get(RequestContext::class)->getPathInfo()); | |
$this->getContainer()->get('request') | |
->attributes->add($params); | |
/** @var Request $request */ | |
$request = $this->getContainer()->get('request'); | |
$controller = $controllerResolver | |
->getController($request); | |
$arguments = $argumentResolver | |
->getArguments($request, $controller); | |
call_user_func_array($controller, $arguments); | |
} | |
private function getContainer(): ContainerBuilder | |
{ | |
if (!$this->container) { | |
$this->container = new ContainerBuilder(); | |
$loader = new YamlFileLoader( | |
$this->container, | |
new FileLocator(__DIR__ . '/../config') | |
); | |
$loader->load('services.yaml'); | |
$this->container->compile(true); | |
} | |
return $this->container; | |
} | |
} |
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
content.edit: | |
path: /post/edit/{id} | |
controller: Legacy\Controller\PostController::edit | |
methods: GET | |
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
services: | |
_defaults: | |
public: true | |
autowire: true | |
autoconfigure: true | |
Legacy\: | |
resource: '../src/*' | |
exclude: '../src/{App.php}' | |
request: | |
class: Symfony\Component\HttpFoundation\Request | |
factory: [ Symfony\Component\HttpFoundation\Request, createFromGlobals ] | |
request_stack: | |
class: Symfony\Component\HttpFoundation\RequestStack | |
argument.service.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver | |
argument.attribute.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver | |
argument.default.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver | |
argument.request_value.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver | |
argument.session.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver | |
argument.variadic.resolver: | |
class: Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver | |
Symfony\Component\HttpKernel\Controller\ContainerControllerResolver: | |
Symfony\Component\HttpKernel\Controller\ArgumentResolver: | |
arguments: | |
$argumentValueResolvers: | |
- '@argument.attribute.resolver' | |
- '@argument.default.resolver' | |
- '@argument.request_value.resolver' | |
- '@argument.session.resolver' | |
- '@argument.variadic.resolver' | |
- '@argument.service.resolver' | |
config.locator: | |
class: Symfony\Component\Config\FileLocator | |
arguments: ['../config'] | |
router.loader: | |
class: Symfony\Component\Routing\Loader\YamlFileLoader | |
arguments: | |
$locator: '@config.locator' | |
router.router: | |
class: Symfony\Component\Routing\Router | |
arguments: | |
$loader: '@router.loader' | |
$resource: 'routes.yaml' | |
$options: | |
cache_dir: '../cache' | |
Symfony\Component\Routing\RequestContext: | |
calls: | |
- fromRequest: ['@request'] | |
Symfony\Component\Routing\RouteCollection: | |
factory: ['@router.router', 'getRouteCollection'] | |
Symfony\Component\Routing\Matcher\UrlMatcher: |
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 Legacy\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
class PostController extends AbstractController { | |
public function edit(int $id): void | |
{ | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment