概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:52:10 | 193 次阅读 | 评论: 0 | 来源: 网络整理

A custom route loader allows you to add routes to an application without including them, for example, in a YAML file. This comes in handy when you have a bundle but don’t want to manually add the routes for the bundle to app/config/routing.yml. This may be especially important when you want to make the bundle reusable, or when you have open-sourced it as this would slow down the installation process and make it error-prone.

Alternatively, you could also use a custom route loader when you want your routes to be automatically generated or located based on some convention or pattern. One example is the FOSRestBundle where routing is generated based off the names of the action methods in a controller.

注解

There are many bundles out there that use their own route loaders to accomplish cases like those described above, for instance FOSRestBundle, JMSI18nRoutingBundle, KnpRadBundle and SonataAdminBundle.

Loading Routes

The routes in a Symfony application are loaded by the DelegatingLoader. This loader uses several other loaders (delegates) to load resources of different types, for instance YAML files or @Route and @Method annotations in controller files. The specialized loaders implement LoaderInterface and therefore have two important methods: supports() and load().

Take these lines from the routing.yml in the AcmeDemoBundle of the Standard Edition:

# src/Acme/DemoBundle/Resources/config/routing.yml
_demo:
    resource: "@AcmeDemoBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo

When the main loader parses this, it tries all the delegate loaders and calls their supports() method with the given resource (@AcmeDemoBundle/Controller/DemoController.php) and type (annotation) as arguments. When one of the loader returns true, its load() method will be called, which should return a RouteCollection containing Route objects.

Creating a custom Loader

To load routes from some custom source (i.e. from something other than annotations, YAML or XML files), you need to create a custom route loader. This loader should implement LoaderInterface.

The sample loader below supports loading routing resources with a type of extra. The type extra isn’t important - you can just invent any resource type you want. The resource name itself is not actually used in the example:

namespace AcmeDemoBundleRouting;

use SymfonyComponentConfigLoaderLoaderInterface;
use SymfonyComponentConfigLoaderLoaderResolverInterface;
use SymfonyComponentRoutingRoute;
use SymfonyComponentRoutingRouteCollection;

class ExtraLoader implements LoaderInterface
{
    private $loaded = false;

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

        $routes = new RouteCollection();

        // prepare a new route
        $path = '/extra/{parameter}';
        $defaults = array(
            '_controller' => 'AcmeDemoBundle:Demo:extra',
        );
        $requirements = array(
            'parameter' => 'd+',
        );
        $route = new Route($path, $defaults, $requirements);

        // add the new route to the route collection:
        $routeName = 'extraRoute';
        $routes->add($routeName, $route);

        $this->loaded = true;

        return $routes;
    }

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

    public function getResolver()
    {
        // needed, but can be blank, unless you want to load other resources
        // and if you do, using the Loader base class is easier (see below)
    }

    public function setResolver(LoaderResolverInterface $resolver)
    {
        // same as above
    }
}

注解

Make sure the controller you specify really exists.

Now define a service for the ExtraLoader:

  • YAML
    services:
        acme_demo.routing_loader:
            class: AcmeDemoBundleRoutingExtraLoader
            tags:
                - { name: routing.loader }
    
  • 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_demo.routing_loader" class="AcmeDemoBundleRoutingExtraLoader">
                <tag name="routing.loader" />
            </service>
        </services>
    </container>
    
  • PHP
    use SymfonyComponentDependencyInjectionDefinition;
    
    $container
        ->setDefinition(
            'acme_demo.routing_loader',
            new Definition('AcmeDemoBundleRoutingExtraLoader')
        )
        ->addTag('routing.loader')
    ;
    

Notice the tag routing.loader. All services with this tag will be marked as potential route loaders and added as specialized routers to the DelegatingLoader.

Using the custom Loader

If you did nothing else, your custom routing loader would not be called. Instead, you only need to add a few extra lines to the routing configuration:

  • YAML
    # app/config/routing.yml
    AcmeDemoBundle_Extra:
        resource: .
        type: extra
    
  • XML
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <import resource="." type="extra" />
    </routes>
    
  • PHP
    // app/config/routing.php
    use SymfonyComponentRoutingRouteCollection;
    
    $collection = new RouteCollection();
    $collection->addCollection($loader->import('.', 'extra'));
    
    return $collection;
    

The important part here is the type key. Its value should be “extra”. This is the type which the ExtraLoader supports and this will make sure its load() method gets called. The resource key is insignificant for the ExtraLoader, so it is set to ”.”.

注解

The routes defined using custom route loaders will be automatically cached by the framework. So whenever you change something in the loader class itself, don’t forget to clear the cache.

More advanced Loaders

In most cases it’s better not to implement LoaderInterface yourself, but extend from Loader. This class knows how to use a LoaderResolver to load secondary routing resources.

Of course you still need to implement supports() and load(). Whenever you want to load another resource - for instance a YAML routing configuration file - you can call the import() method:

namespace AcmeDemoBundleRouting;

use SymfonyComponentConfigLoaderLoader;
use SymfonyComponentRoutingRouteCollection;

class AdvancedLoader extends Loader
{
    public function load($resource, $type = null)
    {
        $collection = new RouteCollection();

        $resource = '@AcmeDemoBundle/Resources/config/import_routing.yml';
        $type = 'yaml';

        $importedRoutes = $this->import($resource, $type);

        $collection->addCollection($importedRoutes);

        return $collection;
    }

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

注解

The resource name and type of the imported routing configuration can be anything that would normally be supported by the routing configuration loader (YAML, XML, PHP, annotation, etc.).

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务