- Symfony Tutorial
- Symfony - Home
- Symfony - Introduction
- Symfony - Installation
- Symfony - Architecture
- Symfony - Components
- Symfony - Service Container
- Symfony - Events & EventListener
- Symfony - Expression
- Symfony - Bundles
- Creating a Simple Web Application
- Symfony - Controllers
- Symfony - Routing
- Symfony - View Engine
- Symfony - Doctrine ORM
- Symfony - Forms
- Symfony - Validation
- Symfony - File Uploading
- Symfony - Ajax Control
- Cookies & Session Management
- Symfony - Internationalization
- Symfony - Logging
- Symfony - Email Management
- Symfony - Unit Testing
- Symfony - Advanced Concepts
- Symfony - REST Edition
- Symfony - CMF Edition
- Complete Working Example
- Symfony Useful Resources
- Symfony - Quick Guide
- Symfony - Useful Resources
- Symfony - Discussion
Creating a Simple Web Application
This chapter explains how to create a simple application in Symfony framework. As discussed earlier, you know how to create a new project in Symfony.
We can take an example of “student” details. Let’s start by creating a project named “student” using the following command.
symfony new student
After executing the command, an empty project is created.
Controller
Symfony is based on the Model-View-Controller (MVC) development pattern. MVC is a software approach that separates application logic from presentation. Controller plays an important role in the Symfony Framework. All the webpages in an application need to be handled by a controller.
DefaultController class is located at “src/AppBundle/Controller”. You can create your own Controller class there.
Move to the location “src/AppBundle/Controller” and create a new StudentController class.
Following is the basic syntax for StudentController class.
StudentController.php
namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Response; class StudentController { }
Now, you have created a StudentController. In the next chapter, we will discuss more about the Controller in detail.
Create a Route
Once the Controller has been created, we need to route for a specific page. Routing maps request URI to a specific controller's method.
Following is the basic syntax for routing.
namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class StudentController { /** * @Route("/student/home") */ public function homeAction() { return new Response('Student details application!'); } }
In the above syntax, @Route(“/student/home”) is the route. It defines the URL pattern for the page.
homeAction() is the action method, where you can build the page and return a Response object.
We will cover routing in detail in the upcoming chapter. Now, request the url “http://localhost:8000/student/home” and it produces the following result.