Programming

Create Custom classes And functions in Laravel

Create classes and functions in Laravel

PHP is an object-oriented programming language, and we can define classes and functions like any other OOP language. Laravel is a PHP framework and provides great advantages over other PHP frameworks.

We can easily import Laravel classes anywhere in the application. It is equally easy to do if you are thinking of creating your own classes and functions in Laravel.

One of the reasons we may want to create our own classes is to have customized functionality in our Laravel app. Though we can write code in the controllers, it’s recommended to extract extra functionality from controllers to other classes or functions. This way, we can keep controllers clean and more focused on the main task.

Classes can also be used to reuse the code over and over again in your application. For example, I create custom classes to generate user notifications and initiate the notification class wherever I want.

Create Classes & functions in Laravel

It is better to create custom classes in a separate directory. Create a directory inside the app directory. For the illustration, I am going to create a Classes directory in the app.

+– app
| +– Classes

To create a class that generates users’ notifications, let’s create Notification.php class inside the Classes directory.

+– app
| +– Classes
| +– Notification.php

<?php 
class Notifications {
    public function __construct() {
        return "construct function was initialized.";
    }
    public function create() {
        // create notification
        // send email
        // return output
    }
}

Using Custom Classes in application

Namespace was introduced in PHP 5.3 and allowed to partition code into logical groups. Using namespace, we can use classes anywhere in our application.

To create a namespace, use the keyword ‘namespace’, and all code below this namespace is available under this namespace. So the ‘Notification.php’ class I created above, we can use namespacing to make it usable in the controllers or anywhere else in the application.

<?php 
namespace App\Classes;
class Notifications {
    public function __construct() {
        return "construct function was initialized.";
    }
    public function create() {
        // create notification
        // send email
        // return output
    }
}

As you can notice, the namespace is in the third line. The namespace is App\Classes so any class in the Classes directory is available under App\Classes namespace.

To use this class in the controller, import the class by calling it by its namespace.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Classes\Notification;
class UsersController extends Controller
{
    // activate user subscription
    public function subscribe(Request $request) {
        // activate user subscription
        all subscription code goes here...
        // send notifications
        $notification = new Notification;
        $notification->create();
    }
}

Namespacing is one way of using our custom-created classes in Laravel. It is recommended to use namespace to manage your code easily under logical groups. The other method is to use custom classes is to add path in composer.json file. Add the class path to ‘classmap’.

"autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "app/Classes"
        ]
    },



1 comment

Jorge Ceyca March 28, 2021 at 10:36 am

good example but you forget run composer dump-autoload after change composer.json

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.