Controllers

Contents
Return JSON
Render templates
Redirects
Read the request

Return JSON

APIs return data instead of HTML. The json helper converts a PHP array into a JSON response with the correct headers:

namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; class ApiController extends AbstractController { public function status(): Response { return $this->json(["hello" => "world", "total" => 3]); } }

Render templates

HTML pages come from templates. The render helper loads a Twig file and passes variables into it. The template name counts from the templates/ folder:

namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; class PageController extends AbstractController { public function page(): Response { return $this->render("hello/index.html.twig", ["name" => "Ada"]); } }

Redirects

Old addresses forward to new ones with a redirect response. Pass a route name and Symfony builds the target URL for you:

namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; class ForwardController extends AbstractController { public function legacy(): Response { return $this->redirectToRoute("app_home"); } }

Read the request

Read input through the Request object instead of superglobals. Type-hint it in the method signature and Symfony hands the current request over:

namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class SearchController extends AbstractController { public function search(Request $request): Response { $q = $request->query->get("q", ""); return new Response("Search: " . $q); } }

Query values arrive as strings, and the second argument of get is the default when the key is missing.

Next: Twig basics

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

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: