Skip to content

[WIP] Route building in the kernel #15948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

weaverryan
Copy link
Member

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? no
Fixed tickets Partially #15864
License MIT
Doc PR not yet

WIP, because it depends on the unmerged PR's #15742, #15778 and #15820. But since this completes the picture (and I'm pushing this for 2.8), I needed to get this PR created.

The idea is to make a "normal" kernel look like this: https://gist.github.com/weaverryan/e4b19cabb1d9286c4217#file-smallkernel-php, which has some nice benefit that routing_dev.yml isn't needed anymore, because you can import those routes conditionally here.

But, you could also create an entire kernel without external files. A complex example (probably more complex than you'd have without using external files) is here: https://gist.github.com/weaverryan/e4b19cabb1d9286c4217#file-appkernel-php.

Thanks!

public function getRouteCollection(LoaderInterface $loader)
{
$routes = new RouteCollectionBuilder($loader);
$routes->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like some test code left here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely did - fixed now

@weaverryan weaverryan force-pushed the framework_kernel_with_route_loading branch from aa2ac38 to 662ceec Compare September 27, 2015 23:13
@fabpot
Copy link
Member

fabpot commented Sep 29, 2015

I don't like the mixed-responsibilities here and the fact that the kernel implements the RouteLoaderInterface and the hack to make it work. We already have everything you need in Symfony today to make it simpler and cleaner (hopefully :)):

<?php

namespace Symfony\Bundle\FrameworkBundle\Kernel;

use Symfony\Bundle\FrameworkBundle\Routing\RouteCollectionBuilder;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\MicroKernel as BaseMicroKernel;

abstract class MicroKernel extends BaseMicroKernel
{
    abstract protected function configureRoutes(RouteCollectionBuilder $routes);

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(function (ContainerBuilder $container) use ($loader) {
            $container->prependExtensionConfig('framework', array(
                'router' => array(
                    'resource' => function () use ($loader) {
                        return $this->configureRoutes(new RouteCollectionBuilder($loader))->build();
                    },
                    'type' => 'closure',
                ),
            ));
        });

        parent::registerContainerConfiguration($loader);
    }
}

@fabpot
Copy link
Member

fabpot commented Sep 29, 2015

Of course, my example does not work in PHP 5.3, but adding a $that = $this would be enough to make compatible.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2015

I'm going to submit a PR soon with my own version of a micro-kernel.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2015

That does not work right now in Symfony as we try to dump the container to PHP (and XML in dev mode); so the router resource being a closure, it explodes.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2015

see #15990

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->prependExtensionConfig('framework', array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadFromExtension is enough here as we are always the first to be called.

@fabpot fabpot mentioned this pull request Sep 29, 2015
@fabpot
Copy link
Member

fabpot commented Oct 1, 2015

Closes in favor of #15990

@fabpot fabpot closed this Oct 1, 2015
@weaverryan weaverryan deleted the framework_kernel_with_route_loading branch October 1, 2015 21:15
fabpot added a commit that referenced this pull request Nov 5, 2015
This PR was merged into the 2.8 branch.

Discussion
----------

added a micro kernel

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Related to #15948 and #15820

Commits
-------

eab0f0a added a micro kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants