Load Symfony routes from a static method.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.gitignore
LICENSE
README.md
composer.json
phpunit.xml

README.md

Static Method Loader

A custom route loader for Symfony's routing component that will load routes from a static method.

Written for a tutorial on custom route loaders.

Usage with Symfony Full Stack

Add the StaticMethodLoader to your service configuration and tag it with routing.loader.

# services.yml

services:
    app.static_method_loader:
        class: Chrisguitarguy\StaticMethodLoader\StaticMethodLoader
        tags:
            - { name: routing.loader }

Then import static method resources:

# routing.yml

_other:
    resource: Vendor\Package\ClassName::method
    type: staticmethod

Use Bundles in Class Name

You'll need to add the BundleAwareResolver to the StaticMethodLoader.

# services.yml

services:
    app.static_method_resolver:
        class: Chrisguitarguy\StaticMethodLoader\Resolver\BundleAwareResolver
        arguments ["@kernel"]
    app.static_method_loader:
        class: Chrisguitarguy\StaticMethodLoader\StaticMethodLoader
        arguments: ["@app.static_method_resolver"]
        tags:
            - { name: routing.loader }

Usage with Only the Routing Component

Pass the StaticMethodLoader to your main Router class or pass it to a DelegatingLoader.

use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Chrisguitarguy\StaticMethodLoader\StaticMethodLoader;

// using only the static method loader
$router = new Router(new StaticMethodLoader(), 'Vendor\Package\ClassName::load', [
    'resource_type' => 'staticmethod',
]);

// or with a DelegatingLoader
$router = new Router(new DelegatingLoader( new LoaderResolver([
    new YamlFileLoader(),
    new StaticMethodLoader(),
])), 'path/to/routing.yml');

License

MIT