Javatpoint Logo
Javatpoint Logo

What are the Controllers?

Laravel controllers are an essential feature in a Laravel framework. Initially, we were handling the request logic in the form of closures in route files; now, in place of using closures in route files, we use controller classes. Controllers are used to handle the request logic within the single class, and the controllers are defined in the "app/http/Controllers" directory. Laravel framework follows the MVC (Model View Controller) architecture in which controllers act as moving the traffic back and forth between model and views.

The default file of controller is available in the app/http/Controllers directory.

In the above code, the namespace is used as it allows you to use the same function names and classes in the different parts of the same application. For example,

Suppose we have to run the function having the name, i.e., RunQuery(). They are available in different directories functions1 and functions2, so we can say that namespace avoids the collision between the same function names.

'use' is used to import the class to the current file.

Let's see how to create the controller through Git Bash Window.

Step 1: Open the Git Bash Window and type the command "php artisan make:Controller PostsController" in Git Bash Window to create the Controller.

Laravel Controllers

The above screen shows that the controller named as PostsController has been created successfully.

Step 2: Now move to your project and see whether the PostsController file has been created or not. The path of the file is:

C:\xampp\htdocs\laravelproject\app\Http\Controllers

Laravel Controllers

The above screen shows that the PostsController file is created.

The default code of PostsController.php file is given below:

The above code contains the class that extends the Controller class, but this class does not contain the functions such as create, update, or delete. Now we will see how to create the controller which contains some default functions.

To create the Controller, we will first delete the PostsController.php from the project, which we have created in the previous step.

Type the command:

php artisan make:controller -resource PostController, this command is used to create the controller.

Laravel Controllers

Now, move to your project to see whether the PostController file has been created or not. The path of the file would be:

C:\xampp\htdocs\laravelproject\app\Http\Controllers

Laravel Controllers

The above screen shows that PostController file is created successfully.

The default code of the PostController.php file is given below:

The above code contains the functions which are used to perform the various operations on the resources such as:

create(): It is used to create a new resource.

store(): It is used to store the specified resource.

update(): It is used to update the specified resource in the storage.

destroy(): It is used to remove the specified resources from the storage.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA