Symfony2动态路由 - 缓存问题 [英] Symfony2 dynamic routing - caching issue

查看:227
本文介绍了Symfony2动态路由 - 缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建动态路线,因为我创建了一个CMS,其中创建的每个页面都可以与路由相关联。我正在使用此链接的示例 - http:// php-和-symfony.matthiasnoback.nl/2012/01/symfony2-dynamically-add-routes/ ,并且一切正常,但是路由被缓存,因此一个路由将工作,但是下一个路由将不会,除非我清除缓存。是否可以在此阶段删除路由缓存,还是有其他选择?我不想删除每个页面加载的整个缓存目录,因为这是没有意义的。以下是示例代码:

 命名空间Acme\RoutingBundle\Routing; 

使用Symfony\Component\Config\Loader\LoaderInterface;
使用Symfony\Component\Config\Loader\LoaderResolver;
使用Symfony\Component\Routing\Route;
使用Symfony\Component\Routing\RouteCollection;

class ExtraLoader实现LoaderInterface
{
private $ loaded = false;新新p新新新旗新新新新新旗新新旗新新旗旗哨旗新新新新新新旗新新旗200新新新新旗新新名: \\ RuntimeException('不要加载这个加载器两次');
}

$ routes = new RouteCollection();

$ pattern ='/ extra';
$ defaults = array(
'_controller'=>'AcmeRoutingBundle:Demo:extraRoute',
);

$ route = new Route($ pattern,$ defaults);
$ routes-> add('extraRoute',$ route);

return $ routes;
}

public function supports($ resource,$ type = null)
{
return'extra'=== $ type;
}

public function getResolver()
{
}

public function setResolver(LoaderResolver $ resolver)
{$ 200新新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗新旗新旗新旗新旗新旗新旗新旗新旗新新新新旗新新旗新新旗新新旗新新旗新旗新旗新旗新旗新新新新旗新旗新旗新旗新旗新旗新旗新旗新旗新新新新新旗新新旗新旗新旗新旗新旗新新新新旗新新旗新旗新旗新旗新旗新旗新新旗新旗新旗新新新新旗新新旗新新旗新旗新旗新旗新旗新旗新新新新旗新新旗新旗新旗新旗新旗新旗新新新新旗新旗新旗新旗新旗新旗新新旗新新新旗新旗新旗新旗新旗新新旗新X-454545 X- 20045 X- 20045 X-454545 X- 20045 X-454545 X-454545新新新新旗新新新新旗新新旗新新旗新新旗新新旗200新新新旗新新旗200新新新旗新新旗200新新新旗新新旗新旗新旗新旗新旗旗下旗新新新旗新旗新旗新旗新旗新旗新新旗新旗新旗新旗新旗新Acme / RoutingBundle / Resources / config / services.xml - >
<?xml version =1.0?>
< container xmlns =http://symfony.com/schema/dic/services
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance$新新新新旗新新新旗新新旗新新旗200新新新新旗新新旗新新旗200新新200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 CEolol,

< services> X-454545 X-4545 X-4545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X-
< tag name =routing.loader>< / tag>
< / service>
< / services>
< / container>

我们需要的最后一件事是/app/config/routing.yml中的一些额外的行:

  AcmeRoutingBundle:
resource:。 X- 20045454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- ,因为每个新路由都必须清除缓存,所以你将被hdd / ssd绑定到无用的清除缓存。
另一种方法是在控制器中创建一个新方法,该方法接受GET的动态页面,并以枝条显示动态内容。



您可以创建一个服务呈现动态页面,这将简化事情。


I'm trying to create dynamic routes as I have created a CMS where each page created can be associated to a route. I'm using the example from this link - http://php-and-symfony.matthiasnoback.nl/2012/01/symfony2-dynamically-add-routes/ and all works fine, however the routing is cached, therefore one route will work but then the next won't unless I clear the cache. Is it possible to remove just the routing cache at this stage or is there another alternative? I don't want to remove the whole cache directory on each page load as that wouldn't make sense. Here is the example code:

namespace Acme\RoutingBundle\Routing;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class ExtraLoader implements LoaderInterface
{
private $loaded = false;

public function load($resource, $type = null)
{
    if (true === $this->loaded) {
        throw new \RuntimeException('Do not add this loader twice');
    }

    $routes = new RouteCollection();

    $pattern = '/extra';
    $defaults = array(
        '_controller' => 'AcmeRoutingBundle:Demo:extraRoute',
    );

    $route = new Route($pattern, $defaults);
    $routes->add('extraRoute', $route);

    return $routes;
}

public function supports($resource, $type = null)
{
    return 'extra' === $type;
}

public function getResolver()
{
}

public function setResolver(LoaderResolver $resolver)
{
    // irrelevant to us, since we don't need a resolver
}
}

Then I've made a service for the ExtraLoader:

<!-- in /src/Acme/RoutingBundle/Resources/config/services.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services       http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="acme.routing_loader" class="Acme\RoutingBundle\Routing\ExtraLoader">
            <tag name="routing.loader"></tag>
        </service>
    </services>
</container>

The last thing we need, is a few extra lines in /app/config/routing.yml:

AcmeRoutingBundle:
    resource: .
    type: extra

解决方案

This is quite inefficient, because with each new route you have to clear cache, so you'll be bound by hdd/ssd with useless clear cache. The alternative is to create a new method in controller which accepts a dynamic page on GET and to show the dynamic content in twig.

You can create a service to render the dynamic pages, which will simplify things.

这篇关于Symfony2动态路由 - 缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆