网页
综合
更新时间 2026-11-13 17:27:11
Create your First Page in Symfony (Symfony Docs)
自动蜘蛛
symfony.com
正文约 4962 字
Creating a new page - whether it's an HTML page or a JSON endpoint - is a two-step process:
Do you prefer video tutorials? Check out the Cosmic Coding with Symfony screencast series.
Symfony embraces the HTTP Request-Response lifecycle. To find out more, see Symfony and HTTP Fundamentals.
Before continuing, make sure you've read the Setup article and can access your new Symfony app in the browser.
Suppose you want to create a page - /lucky/number - that generates a lucky (well, random) number and prints it. To do that, create a "Controller" class and a "number" method inside of it:
Now you need to associate this controller function with a public URL (e.g. /lucky/number) so that the number() method is called when a user browses to it. This association is defined with the #[Route] attribute (in PHP, attributes are used to add metadata to code):
That's it! If you are using the Symfony web server, try it out by going to: http://localhost:8000/lucky/number
Symfony recommends defining routes as attributes to have the controller code and its route configuration at the same location. However, if you prefer, you can define routes in separate files using the YAML or PHP formats.
If you see a lucky number being printed back to you, congratulations! But before you run off to play the lottery, check out how this works. Remember the two steps to create a page?
Your project already has a powerful debugging tool inside: the bin/console command. Try running it:
You should see a list of commands that can give you debugging information, help generate code, generate database migrations and a lot more. As you install more packages, you'll see more commands.
To get a list of all of the routes in your system, use the debug:router command:
You should see your app_lucky_number route in the list:
You will also see debugging routes besides app_lucky_number -- more on the debugging routes in the next section.
You'll learn about many more commands as you continue!
If your shell is supported, you can also set up console completion support. This autocompletes commands and other input when using bin/console. See the Console document for more information on how to set up completion.
One of Symfony's amazing features is the Web Debug Toolbar: a bar that displays a huge amount of debugging information along the bottom of your page while developing. This is all included by default when using a Symfony pack called symfony/profiler-pack.
You will see a dark bar along the bottom of the page. You'll learn more about all the information it holds along the way, but feel free to experiment: hover over and click the different icons to get information about routing, performance, logging and more.
If you're returning HTML from your controller, you'll probably want to render a template. Fortunately, Symfony comes with Twig: a templating language that's minimal, powerful and actually quite fun.
Install the twig package with:
Make sure that LuckyController extends Symfony's base AbstractController class:
Now, use the handy render() method to render a template. Pass it a number variable so you can use it in Twig:
Template files live in the templates/ directory, which was created for you automatically when you installed Twig. Create a new templates/lucky directory with a new number.html.twig file inside:
The {{ number }} syntax is used to print variables in Twig. Refresh your browser to get your new lucky number!
http://localhost:8000/lucky/number
Now you may wonder where the Web Debug Toolbar has gone: that's because there is no </body> tag in the current template. You can add the body element yourself, or extend base.html.twig, which contains all default HTML elements.
In the templates article, you'll learn all about Twig: how to loop, render other templates and leverage its powerful layout inheritance system.
Great news! You've already worked inside the most important directories in your project:
Most of the time, you'll be working in src/, templates/ or config/. As you keep reading, you'll learn what can be done inside each of these.
So what about the other directories in the project?
And when you install new packages, new directories will be created automatically when needed.
Congrats! You're already starting to learn Symfony and discover a whole new way of building beautiful, functional, fast and maintainable applications.
OK, time to finish learning the fundamentals by reading these articles:
Then, learn about other important topics like the service container, the form system, using Doctrine (if you need to query a database) and more!
Online Symfony certification, take it now!
Peruse our complete Symfony & PHP solutions catalog for your web development needs.
Thanks Marco Lipparini (@liarco) for being a Symfony contributor
2 commits • 11 lines changed
View all contributors that help us make Symfony
Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。