Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create Custom Class or Custom Library in Laravel 11

Create Custom Class or Custom Library in Laravel 11

Aman Jain, July 6, 2024July 6, 2024

Custom classes and libraries in Laravel can streamline your application’s codebase by encapsulating common functionality and business logic. This tutorial guides you through creating and utilizing custom classes using Laravel’s powerful features. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. In 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, let’s create a helper class file within the Laravel application. You can place it in the app folder directly or organize it under a new folder like Lib inside app. Here’s an example creating a file app/Lib/PaymentHelper.php:

<?php 
namespace \App\Lib;

class PaymentHelper{

public $key='test_key';

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

}

Step 2 : Create Routes

Next, define routes to access and test our custom class. You can do this directly in a route file or within a controller. Here are examples of both approaches

<?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 You can now use your custom class in your application by importing it and creating a new instance of it. For example, in a controller:

<?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.

Pro Tips:

To make your custom class globally accessible across the application, you can register it within Laravel’s AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\MyCustomClass;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('myCustomClass', function () {
            return new MyCustomClass();
        });
    }
}

You can now use your custom class or library class anywhere in your application by calling the global helper function app() and passing in the name of the class you registered. For example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    public function index(Request $request)
    {
        $customClass = app('myCustomClass');

        // Use your custom class here
        $customClass->myCustomFunction();
    }
}

NOTE: This Singlton method will only create the object for once and will use the same object on every injection.
This concludes the tutorial on creating and using custom classes or libraries effectively within Laravel. By following these steps, you can enhance code organization and reuse across your Laravel applications.

Related

Php Laravel Laravel 11 classhelperlaravelphp

Post navigation

Previous post
Next post

Related Posts

Laravel 11 understanding high vulnerabilities in software a week of insights

Understanding High Vulnerabilities in Software: A Week of Insights

May 4, 2025

In the fast-paced world of technology, vulnerabilities in software can emerge at any moment, posing risks to users and organizations alike. The week of April 21, 2025, was marked by the publication of several high vulnerabilities that caught the attention of developers, security professionals, and businesses globally. Each vulnerability brings…

Read More
Laravel Call Controller Method from Another Controller in Laravel

How to Call Controller Method from Another Controller in Laravel ?

December 2, 2023March 16, 2024

In this article we will learn Call Controller Method from Another Controller in Laravel. Sometimes we need to access the controller method from another controller to save the same code on multiple locations. Best way to achieve it is using create a separate service class or trait in PHP so…

Read More
Php How to Send Mail with Attachment(PDF, docs,image) in Laravel

How to Send Mail with Attachment(PDF, docs,image) in Laravel 8 /9 ?

May 10, 2022May 10, 2022

Attachment with mail is very important feature of mail by which we can attach any document with our email , for example in some scenarios we want to send the invoice to user mail or some guideline to user after registration. So in this article we will learn to use…

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