Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Creating controller in laravel

How to create controller in Laravel 8?

Aman Jain, November 7, 2021November 5, 2023

In Laravel controller is most important part to create the connection between our business logic to our view. we will learn create controller in laravel 8.Laravel is a MVC pattern and C stands for controller.

It’s responsible for to receive the input from user, process them and validate the input then it send to model and business logic of our application.

Default directory for controllers in Laravel 8 is app\Http\Controllers.

Let’s create a controller in Laravel 8

Create controller in laravel 8

There is 2 ways to create the controller

  1. Using Artisan command
  2. Creating controller manually in directory

Below is the command to create the controller using artisan

php artisan make:controller TestController

Also we can create manually in file system below file /app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    function index(){
     return "Hello";
    }
}

In the same way you can create a controller in subfolder

Create a controller in subfolder

php artisan make:controller Api/TestController

It will create the controller in app/Http/Controllers/Api/TestController.php 

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class TestController extends Controller
{
    //
}

Define the route for controller

We have successfully created our controller but route is not created yet for accessing for controller from URL, So create a route in route/web.php

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\TestController;

Route::get('/test',[TestController::class, 'index']);

Here, we imported TestController and then used in our get route. Whenever a request exact match with /test it will call the TestController index method with specified params.

Parametrized Route for controller

In above example we called a simple route to serve the request, now we will create a route with param

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\TestController;

Route::get('/test/{id}',[TestController::class, 'index']);

Here, we used {id} as paramo so our controller method will accept a param

 function index($id){
     return "Hello world $id";
 }

Creating Controller Middleware

In our last article Laravel middleware we demonstrate how to create and use middleware, now we can also use the middleware in controller.

There is 2 ways to use middleware in controller.

  1. In Route
  2. In Same Controller

Route Middleware

Route::get('/test', [TestController::class, 'index'])->middleware('auth');

In controller middleware we can define in same controller using $this->middleware in constructor

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('log')->only('index');
     
    }

    function index(){
        
    }
}

I hope you find it well…

beenhere

ALSO READ

How to create resource controller

Related

Php Laravel controllerlaravel

Post navigation

Previous post
Next post

Related Posts

Php How to Send Mail in Laravel Through Sendgrid

How to Send Mail in Laravel 8 / 9 Through Sendgrid ?

May 10, 2022May 10, 2022

Sendgrid Provides multiple features to send the email like bulk email, marketing email , APIs, Delivery reports, Templates and many more. In that way send gird is much interesting and capable of performing many tasks related to marketing and bulk mailing. So in this article we will learn to use…

Read More
Php Ajax laravel Image Upload

Ajax laravel Image Upload with form with example

June 6, 2022November 6, 2023

In this tutorial i will show you to use Ajax laravel image Upload with form in laravel . This can be implement easily using the laravel file and storage providers. in our recent articles of How to Upload image with preview in Laravel 8 with example ? i explained to…

Read More
Javascript Laravel Dynamic Autocomplete Using Typehead JS

Laravel Dynamic Autocomplete Using Typehead JS

July 16, 2022July 16, 2022

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version