Skip to content

[FrameworkBundle] Add support for route attributes in kernel controller methods #46115

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

Merged
merged 0 commits into from
Apr 20, 2022

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Apr 19, 2022

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT
Doc PR todo

Simplify creating single file projects using route attributes:

<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Annotation\Route;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    #[Route('/')]
    public function homepage()
    {
        return new Response('Hello, world');
    }
}

$app = new Kernel($_SERVER['APP_ENV'] ?? 'prod', $_SERVER['APP_DEBUG'] ?? false);

if (\PHP_SAPI === 'cli') {
    $application = new Application($app);
    exit($application->run());
}

$request = Request::createFromGlobals();
$response = $app->handle($request);
$response->send();
$app->terminate($request, $response);

This will also allow removing these lines from the FrameworkBundle recipe: https://github.com/symfony/recipes/blob/master/symfony/routing/6.0/config/routes.yaml#L5-L7

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Nice :)
please add a line on the changelog of the component
we now need to update the recipe for symfony/routing 6.1
can you please send a PR ?

@fancyweb
Copy link
Contributor

Should we deprecate declaring routes with the ->controller([$this, 'foo']) or ->controller($this->foo(...)) syntaxes then? I feel like being able to use the Route attribute directly without any configuration fills the same need but in a better way.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Apr 20, 2022

Deprecating always has a cost, which needs to be justified. What would be the justification for the migration cost?

@dunglas
Copy link
Member Author

dunglas commented Apr 20, 2022

The "old" syntax still has use cases: for instance, you may want to register methods as controllers dynamically (using reflection or complex things like this). I would keep it (and it has been supported for years). However, we could update the docs to promote the new syntax.

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Cool

@fabpot
Copy link
Member

fabpot commented Apr 20, 2022

Thank you @dunglas.

@fabpot fabpot closed this Apr 20, 2022
@fabpot fabpot force-pushed the feat/route-attributes-in-kernel branch from 6e35580 to 0ae445c Compare April 20, 2022 13:55
@fabpot fabpot merged commit e740a11 into symfony:6.1 Apr 20, 2022
@dunglas dunglas deleted the feat/route-attributes-in-kernel branch April 20, 2022 14:26
@fabpot fabpot mentioned this pull request Apr 27, 2022
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.

7 participants