Laravel Controllers

In this tutorial you will learn about the Laravel Controllers and its application with practical example.

What is Controller?

In MVC framework, the letter “C” stands for Controller. Controller is the entry point for any MVC framework based application, it receives the incoming HTTP requests and process it communicating with models and views, then return the results back to the web browser. In Laravel, controllers is completely responsible for handling the application logic.

In Laravel, all of the controllers stored in the Controllers folder, located in App/Http/Controllers.

Creating Basic Controller

Controllers can be created using following artisan command –

Syntax:-

Replace <controller-name> with name you like for controller you are creating.

Once the above command is executed, a controller class file with some basic controller code will be created in the controllers folder, located in App/Http/Controllers.

Once the controller created, it can be invoked by defining route in routes.php.

Syntax:-

Example:-

Step 1:- Open the command prompt and execute the following Artisan command –

Step 2:- As the above command is executed, an empty AuthorController class will be generated under App/Http/Controllers with the name AuthorController.php. At this point, your controller should look like as following –

Step 3:- Now create a new method inside the AuthorController to show all posts, method should look like as following –

Step 4:- Create a view file dashboard.php inside resources/views/ directory, and put the following code inside it –

Step 5:- Open app/Http/routes.php and configure the route as below –

Step 6:- Now open the following URL in the browser to see the output.

http://localhost:8000/dashboard

Passing Parameters to Controllers

If you want to pass parameters to controller, it can be done easily in following way –

Step 1:- Open app/Http/routes.php and re-configure the route as below –

Step 2:- Open the “AuthorController.php” file we created above, and update “index” method as following.

Step 3:- Open “dashboard.php” we created above, and update the code as following –

Step 6:- Now open the following URL in the browser to see the output.

http://localhost:8000/dashboard/john

Creating Restful Resource Controllers

In any web application CRUD (Create, Read, Update and Delete) are the basic operations on a resource. With a resource controllers you get a generic controller structure that includes all the methods for performing CRUD operations. In Laravel, using a single command you can create a resource controller. In Laravel,you don’t need setup different routes for CRUD operations. Single route declaration can handle all of the RESTful operations for specified resource.

Create a Resource Controller

In Laravel, the command used to create a resource controller is same as normal controller, just at the end we have to add suffix “resource”.

Once the above command is executed, a controller class file PostController.php with index, create, store, show, edit, update, and destroy methods will be created in the controllers folder, located in App/Http/Controllers.

app/Http/Controllers/PostController.php

Once the controller created, the route declaration for the above resource controller in routes.php looks as following –

This resource route declaration creates multiple routes to handle a variety of RESTful operations on the post resource.

Actions Handled By Resource Controller

Below is the list of different URI’s created for the resource –

HTTP Verb Path (URL) Action (Method) Route Name
GET /post index post.index
GET /post/create create post.create
POST /post store post.store
GET /post/{id} show post.show
GET /post/{id}/edit edit post.edit
PUT/PATCH /post/{id} update post.update
DELETE /post/{id} destroy post.destroy

Partial Resource Routes

We can specify a resource route to handle set of actions as following –

Similarly, we can exclude the actions that we do not want a resource route to handle, it can be declared as following –

 

In this tutorial we have learn about the Laravel Controllers and its application with practical example. I hope you will like this tutorial.