Your first page

Contents
One route attribute
A tiny controller
See it in the browser
Change the response

One route attribute

A route connects a URL path to a PHP method. In modern Symfony the connection is an attribute written directly above the method: the path tells which URL matches, and the name gives the route a short label for later use.

use Symfony\Component\Routing\Attribute\Route; #[Route("/hello", name: "app_hello")] function hello(): string { return "hello"; }

A tiny controller

A controller is a plain PHP class whose methods return responses. Save it as src/Controller/HelloController.php so the framework finds it with the rest of your code:

namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; class HelloController extends AbstractController { #[Route("/hello", name: "app_hello")] public function hello(): Response { return new Response("Hello from Symfony!"); } }

See it in the browser

With the server from the previous lesson still running, open http://localhost:8000/hello in a browser. The page shows the string from the controller and nothing else.

A 404 on that address means the path in the browser and the path in the attribute spell the URL differently. Compare the two letter by letter.

Change the response

Edit the string inside the controller, save the file, and refresh the browser. No build step and no restart stand between the change and the result:

return new Response("Hello again, Symfony!");

Next: Routes

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: