Introduction
When starting to learn any new framework, it’s always a good practice to begin with a ‘Hello World’ example. This tutorial will guide you through the steps of setting up a basic ‘Hello World’ app using Symfony, one of the leading frameworks for building web applications in PHP.
Prerequisites
- PHP 7.2.5 or higher installed on your system
- Composer – PHP’s dependency manager
- A text editor or IDE of your choice
- A basic understanding of PHP
Installing Symfony
Symfony can be easily installed on any platform using Composer:
composer create-project symfony/website-skeleton hello_world
This command creates a new directory called ‘hello_world’ with a basic Symfony project within it.
Setting Up the Web Server
Symfony provides a web server bundle that is super handy for development:
composer require symfony/web-server-bundle --dev
To start the server, navigate to your project directory and run:
php bin/console server:run
Your application will be available at ‘http://localhost:8000’.
Creating a Controller
In Symfony, logic is defined within ‘controllers’. Let’s create a controller to handle our ‘Hello World’ output:
php bin/console make:controller HelloWorldController
This will generate a new controller class and a template for us.
Your First Route and Controller
Open the generated controller located at ‘src/Controller/HelloWorldController.php’ and modify the ‘index’ method:
use Symfony\Component\HttpFoundation\Response;
// ...
public function index(): Response
{
return new Response('<html><body>Hello World!</body></html>');
}
Visit ‘http://localhost:8000/hello-world’ to see the output.
Using Templates
We can simplify our controller’s action by returning a rendered template:
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
// ...
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function index(): Response
{
return new Response($this->twig->render('hello_world/index.html.twig'));
}
The template file ‘templates/hello_world/index.html.twig’ may look like this:
<html>
<head><title>Hello World!</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Routes Configuration
Let’s define a route in config/routes.yaml:
hello_world:
path: /hello-world
controller: App\Controller\HelloWorldController::index
This tells Symfony that whenever the ‘/hello-world’ path is hit, it should execute the ‘index’ method on our ‘HelloWorldController’.
Database Connectivity
Although not necessary for a ‘Hello World’ app, it’s useful to know how to connect to a database. This can be achieved by updating the ‘.env’ file with your database credentials and running:
php bin/console doctrine:database:create
This creates a new database that can be used in your Symfony project.
Conclusion
This tutorial provided a step-by-step guide on creating a simple ‘Hello World’ application using Symfony. By following this tutorial, you should now have a basic understanding of Symfony components such as routes, controllers, and views. Keep exploring Symfony’s features to build more complex applications.