Skip to content

[Routing] Add PHP Attribute route example to the controller #15008

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 1 commit into from
Feb 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ a service like: ``App\Controller\HelloController::index``:
}
}

.. code-block:: php-attributes

// src/Controller/HelloController.php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;

class HelloController
{
#[Route('/hello', name: 'hello', methods: ['GET'])]
public function index()
{
// ...
}
}

.. code-block:: yaml

# config/routes.yaml
Expand Down Expand Up @@ -105,6 +121,23 @@ which is a common practice when following the `ADR pattern`_
}
}

.. code-block:: php-attributes

// src/Controller/Hello.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/hello/{name}', name: 'hello')]
class Hello
{
public function __invoke($name = 'World')
{
return new Response(sprintf('Hello %s!', $name));
}
}

.. code-block:: yaml

# config/routes.yaml
Expand Down