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