In Chapter 1, we set up our development environment. Now it’s time for the real work. In this chapter we do two things:
- Install our Symfony 8 project, following the official documentation step by step
- Integrate an HTML/CSS template, so our app looks good from day one
Installing a Symfony 8 Project
We already ran symfony check:requirements in the previous chapter, so our machine is ready. The official documentation gives us two ways to create a project.
Option 1: Symfony CLI (recommended)
symfony new minibib --version=8.* --webapp
The --webapp option installs a traditional web application with every component you need: Twig, Doctrine, forms, security, and more.
Without --webapp, Symfony installs only the minimal skeleton, which is what you’d use for a console application, an API, or a microservice.
Option 2: Composer
composer create-project symfony/skeleton minibib
cd minibib
composer require webapp
This gives the same result in two steps: first a minimal project, then the web application components.
We’ll use the Symfony CLI, since it’s the simplest method. Our project is called minibib. Once it’s created, step into the folder and open it in VS Code:
cd minibib
code .
You now have a complete Symfony project that runs out of the box.
Starting the Local Server
symfony server:start
The app is now available at http://127.0.0.1:8000. The project is still empty, but you’ll already see the Symfony welcome page.
Creating Our First Controller
Our controller will display our template on a specific route.
Two ways to run console commands
Symfony ships with a console that can generate controllers, entities, event subscribers, and much more. You can call it in two equivalent ways:
php bin/console <command>
# or
symfony console <command>
Both run exactly the same commands.
Generating the controller
symfony console make:controller HomeController
We don’t need PHPUnit tests for now, so we answer no when asked. The command creates two files:
| File | Role |
|---|---|
src/Controller/HomeController.php | The PHP logic |
templates/home/index.html.twig | The HTML interface, written in Twig |
Here’s the generated controller:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class HomeController extends AbstractController
{
#[Route('/home', name: 'app_home')]
public function index(): Response
{
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
}
Symfony created a /home route for us. The controller method is where the application logic lives, such as fetching data from the database or running calculations. It then returns the template with render().
Visit http://127.0.0.1:8000/home: the HomeController page shows up.
Integrating the Template
For this project I’m using a free template made of one HTML file and one CSS file. Here’s how to plug it into Symfony.
1. Replace the Twig content
Open templates/home/index.html.twig, remove the generated content, and paste the template’s HTML inside the body block:
{% extends 'base.html.twig' %}
{% block body %}
{# the template's HTML goes here #}
{% endblock %}
2. Add the CSS with AssetMapper
Symfony 8 uses AssetMapper. Copy the template’s CSS into assets/styles/app.css. That file is already loaded automatically through the importmap configuration, so there’s nothing else to wire up.
3. Put global links in base.html.twig
External stylesheets used everywhere in the app, like Bootstrap, belong in templates/base.html.twig:
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.8.1/baguetteBox.min.css">
{% block stylesheets %}{% endblock %}
{% block javascripts %}
{% block importmap %}{{ importmap('app') }}{% endblock %}
{% endblock %}
</head>
The template’s JavaScript files go in base.html.twig too.
Global vs. page-specific: styles and scripts used across the whole app go in
base.html.twig. If a style or script is only needed on one page, add it in that page’s template using thestylesheetsorjavascriptsTwig blocks.
4. Move the images to public/
Copy the template’s images folder into public/. Files in public/ are served directly to the browser, which is exactly what images need.
Refresh the page: the template is integrated and looks exactly like the original.
Recap
- Create a project:
symfony new <name> --webapptakes a few seconds - Start the server:
symfony server:start - Create a controller:
symfony console make:controller - CSS: goes in
assets/(AssetMapper) or inpublic/ - Global styles and scripts go in
base.html.twig, while page-specific code goes in the matching template
Exercise · A second page
Create an “About” page at /about, with its own controller and template, extending base.html.twig.
Show the solution
symfony console make:controller AboutController
Then adjust the generated route:
#[Route('/about', name: 'app_about')]
public function index(): Response
{
return $this->render('about/index.html.twig');
}
And in templates/about/index.html.twig:
{% extends 'base.html.twig' %}
{% block title %}About{% endblock %}
{% block body %}
<h1>About the Mini Library</h1>
{% endblock %}
What’s Next
The template currently opens an image when you click an item. In our library, clicking a book cover will instead lead to a page that shows the book’s PDF content. So we’ve already removed the parts of the template we don’t need, and later we’ll point the links to our own pages.
In the next chapter, we’ll set up the database, entities, and migrations.
This is Chapter 2 of the Mini Library course. Follow along to build a complete Symfony 8 + EasyAdmin digital library application from scratch.