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:-
1 |
php artisan make:controller <controller-name> |
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:-
1 |
Route::get('base URI','controllerName@methodName'); |
Example:-
Step 1:- Open the command prompt and execute the following Artisan command –
1 |
php artisan make:controller AuthorController |
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 –
1 2 3 4 5 6 7 8 9 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class AuthorController extends Controller { // } |
Step 3:- Now create a new method inside the AuthorController to show all posts, method should look like as following –
1 2 3 4 |
public function index() { return view(dashboard); } |
Step 4:- Create a view file dashboard.php inside resources/views/ directory, and put the following code inside it –
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html> <head> <title>Author Dashboard</title> </head> <body> <h2>Welcome Author</h2 </body> </html> |
Step 5:- Open app/Http/routes.php and configure the route as below –
1 |
Route::get('dashboard', 'AuthorController@index'); |
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 –
1 |
Route::get('dashboard/{name}', 'AuthorController@index'); |
Step 2:- Open the “AuthorController.php” file we created above, and update “index” method as following.
1 2 3 4 |
public function index($name) { return view('dashboard',["name"=>$name]); } |
Step 3:- Open “dashboard.php” we created above, and update the code as following –
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html> <head> <title>Author Dashboard</title> </head> <body> <h2>Welcome <?php echo $name;?></h2 </body> </html> |
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”.
1 |
php artisan make:controller PostController --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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class PostController extends Controller { /** * Display a listing of the resource. * * @return Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } } |
Once the controller created, the route declaration for the above resource controller in routes.php looks as following –
1 |
Route::resource('post', 'PostController'); |
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 –
1 2 3 |
Route::resource('post', 'PostController', ['only' => [ 'index', 'show' ]]); |
Similarly, we can exclude the actions that we do not want a resource route to handle, it can be declared as following –
1 2 3 4 5 |
Route::resource('post', 'PostController', ['except' => [ 'create', 'store', 'update', 'destroy' ]]); |