- Complete Working Example
- Symfony - CMF Edition
- Symfony - REST Edition
- Symfony - Advanced Concepts
- Symfony - Unit Testing
- Symfony - Email Management
- Symfony - Logging
- Symfony - Internationalization
- Cookies & Session Management
- Symfony - Ajax Control
- Symfony - File Uploading
- Symfony - Validation
- Symfony - Forms
- Symfony - Doctrine ORM
- Symfony - View Engine
- Symfony - Routing
- Symfony - Controllers
- Creating a Simple Web Application
- Symfony - Bundles
- Symfony - Expression
- Symfony - Events & EventListener
- Symfony - Service Container
- Symfony - Components
- Symfony - Architecture
- Symfony - Installation
- Symfony - Introduction
- Symfony - Home
Symfony Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Creating a Simple Web Apppcation
This chapter explains how to create a simple apppcation in Symfony framework. As discussed earper, 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 apppcation logic from presentation. Controller plays an important role in the Symfony Framework. All the webpages in an apppcation 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 AppBundleController; use SymfonyComponentHttpFoundationResponse; 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 AppBundleController; use SensioBundleFrameworkExtraBundleConfigurationRoute; use SymfonyComponentHttpFoundationResponse; use SymfonyBundleFrameworkBundleControllerController; class StudentController { /** * @Route("/student/home") */ pubpc function homeAction() { return new Response( Student details apppcation! ); } }
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.