Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当使用Symfony路由作为独立路由时,如何缓存路由?_Php_Symfony_Caching_Url Routing - Fatal编程技术网

Php 当使用Symfony路由作为独立路由时,如何缓存路由?

Php 当使用Symfony路由作为独立路由时,如何缓存路由?,php,symfony,caching,url-routing,Php,Symfony,Caching,Url Routing,我单独使用Symfony路由组件,即不使用Symfony框架。以下是我正在使用的基本代码: <?php $router = new Symfony\Component\Routing\RouteCollection(); $router->add('name', new Symfony\Component\Routing\Route(/*uri*/)); // more routes added here $context = new Symfony\Component\Routi

我单独使用Symfony路由组件,即不使用Symfony框架。以下是我正在使用的基本代码:

<?php
$router = new Symfony\Component\Routing\RouteCollection();
$router->add('name', new Symfony\Component\Routing\Route(/*uri*/));
// more routes added here

$context = new Symfony\Component\Routing\RequestContext();
$context->setMethod(/*method*/);
$matcher = new Symfony\Component\Routing\Matcher\UrlMatcher($router, $context);

$result = $matcher->match(/*requested path*/);

Symfony路由组件文档包含一个如何轻松启用缓存的示例:

基本上,您的示例可以按照以下方式重新编写:

// RouteProvider.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();
$collection->add('name', new Route(/*uri*/));
// more routes added here

return $collection;
// Router.php
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\RequestContext
use Symfony\Component\Routing\Loader\PhpFileLoader;

$context = new RequestContext();
$context->setMethod(/*method*/);

$locator = new FileLocator(array(__DIR__));
$router = new Router(
    new PhpFileLoader($locator),
    'RouteProvider.php',
    array('cache_dir' => __DIR__.'/cache'), // must be writeable
    $context
);
$result = $router->match(/*requested path*/);