Friday, August 30, 2019

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.

Result

Symfony Framework

No comments:

Post a Comment

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...