Create Custom 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.

Laravel classes can easily be imported anywhere in the application. If you are thinking to create your own classes and functions in Laravel, it is equally easy to do.

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 but 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 app.

+– app
| +– Classes

To create a class that generates users notifications, let’s create Notification.php class inside 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 allows to partition code into logical groups. Using namespace, we can use classes anywhere in our application.

To create a namespace, use keyword ‘namespace’ and all code below this namespace is avaiblble under this namespace. So the ‘Notification.php’ class I created above, we can use name spacing 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 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 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();
    }
}

Name spacing 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"
        ]
    },


from LinuxAndUbuntu – Linux Tutorials, FOSS Reviews, Security News https://ift.tt/31MABRJ

Post a Comment

0 Comments