English 中文(简体)
Creating a Simple Web Application
  • 时间:2024-12-22

Creating a Simple Web Apppcation


Previous Page Next Page  

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.

Result

Symfony Framework Advertisements