In this tutorial, we discuss about Laravel Controller, how to make controller, create function in laravel controller, call controller method from routing and pass parameters with URL.
What is Laravel Controller
A controller is the central unit of laravel, which handle all request sending by route files. A controller can manage group-related route requests into a single class.
For example LoginController is controller class, which handle request to showing login html form, register users, forgot password.
By default laravel controller file stored in the app/Http/Controllers directory.
How to Make Controller Laravel 8
We are showing an example of Laravel 8 version controller.
To create an controller type below command in your laravel root directory using command prompt.
1 |
php artisan make:controller LoginController |
In the above command ‘LoginController’ is the name of the controller file that you want to create.
Create a function in Laravel 8 Controller
After this command, you will that LoginController.php file is created under app/Http/controllers folder.
Now we are creating a show() function in this controller which will display some text.
The basic structure of a controller looks like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class LoginController extends Controller { // this is dummy show function that we created inside this controller which echo this text when you execute this method via route. public function show() { return "Hello from the Login Controller"; } } |
Call Controller Method from Routing
Now to call show() function, we need to create a laravel routing method for this controller method.
To create new route, go to in routes/web.php file.
In laravel 8 version, first you have to import this ‘LoginController’ inside this web.php route file. Like this
use App\Http\Contollers\LoginController
Check below route example to call this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\LoginController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get("login", [LoginController::class, 'show']); // here login is the web page url // show is the controller method |
Here “login” is the url parameter, which you execute to call this method like
http://localhost:8080/login
And ‘show’ is the LoginController method name which we defined in controller class.
Pass Parameter using Controller
To pass the data from url to controller method like any Name or Id
http://localhost/login?id=1234
First add the parameter in web.php get route like this
1 |
Route::get("login/{id}", [LoginController::class, 'show']); |
You can multiple parameters here.
Now change in show() function inside ‘LoginController’ file. Here we need to get that ‘id’ parameter value.
1 2 3 4 |
public function show($id) { return $id; } |
This will show id value in the show method, whatever you passed in the web URL.