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.