Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

Create custom library or custom class in Laravel 8 / 9

Aman Jain, December 21, 2021November 6, 2023

Laravel provides various packages and library to enhance our application features and functionalities but sometime we want our custom class in laravel to handle the few features of the application. Thus I this article i will show you to create a class and use it using the alias or directly from the namespace name.

Let’s begin the tutorial of custom helper functions step by step

Step 1 : Create a class file

First step is create a helper file in app folder or we can create a folder named as Lib in app folder then we can create multiple files in that folder. in this example i am going to create a file in app/Lib/PaymentHelper.php

<?php 
namespace \App\Lib;

class PaymentHelper{

public $key='test_key';

  function getConfig(){
    return $this->key;
  }

}

Step 2 : Create Routes

Now, Create a route to check our implementation on controller or route itself using anonymous function.

<?php

use Illuminate\Support\Facades\Route;
 
Route::get('/helper',function(){
   
   $Payment=new \App\Lib\PaymentHelper();
    return $Payment->getConfig();
}); 

Or in controller

<?php

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

 
Route::get('/helper',[ArticleController::class, 'test']); 

Create a controller in app\Http\Controllers\ArticleController and method helper

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
user App\Lib\PaymentHelper;

class ArticleController extends Controller
{
    public function test(Request $request)
    {
        $Payment=new PaymentHelper();
        return $Payment->getConfig();
    }
}
?>

In the same way we can use it in our view file.

Related

Php Laravel classhelperlaravelphp

Post navigation

Previous post
Next post

Related Posts

Php How to run specific seeder class in laravel

How to run specific seeder class in laravel 9 ?

February 19, 2022February 22, 2022

In our last article we learnt about how to create seeder in laravel but sometimes we only need to run a specific seeders rather then running all so in this article we will cover to run a specific seeder. We will use a simple example of article class and will…

Read More
Php How to Add or Update Index in Laravel Migration

How to Add or Update Index in Laravel Migration?

August 21, 2022March 16, 2024

Add or Update Index in Laravel Migration can be implement easily using the migrations libraries index method. Migrations provides almost all feature to create the database schema and create and update index in laravel migration. As We know database index is used to improve the speed of tables. Index can…

Read More
Php Laravel Redirect from Http to Https Secure URL

Laravel Redirect from Http to Https Secure URL

August 26, 2022March 16, 2024

To redirect from http to https secure url in laravel we can achieve by multiple ways. While deploying application to server we need to make our website secure and robust so attackers can not attack easily on website. In this tutorial we will learn to redirect the http to https…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version