27 C
Jaipur
Sunday, October 25, 2020

Create Custom classes And functions in Laravel – LinuxAndUbuntu

Must read

Lenovo Legion Phone Duel, a bizarre new gaming phone, is available to buy now

If you're looking to buy a new gaming phone the Lenovo Legion Phone Duel is out today, and it might be great for the...

Packers vs Texans live stream: how to watch NFL week 7 online from anywhere

The Green Bay Packers roll into week 7 with an impressive 4-1 record and all signs point to that becoming 5-1 after today's game...

Steelers vs Titans live stream: how to watch NFL week 7 online from anywhere

Two of the NFL’s three remaining undefeated teams do battle at Heinz Field today in the pick of week 7's games. After a three-week...

YouTube TV Channels In 2020: Cost & Features Explained

When it comes to cord-cutting due to the constant leaps in the cable bill, the first thing that comes to our minds is Netflix...

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 AppClasses;

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 AppClasses so any class in the Classes directory is available under AppClasses namespace.

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

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppClassesNotification;

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

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Lenovo Legion Phone Duel, a bizarre new gaming phone, is available to buy now

If you're looking to buy a new gaming phone the Lenovo Legion Phone Duel is out today, and it might be great for the...

Packers vs Texans live stream: how to watch NFL week 7 online from anywhere

The Green Bay Packers roll into week 7 with an impressive 4-1 record and all signs point to that becoming 5-1 after today's game...

Steelers vs Titans live stream: how to watch NFL week 7 online from anywhere

Two of the NFL’s three remaining undefeated teams do battle at Heinz Field today in the pick of week 7's games. After a three-week...

YouTube TV Channels In 2020: Cost & Features Explained

When it comes to cord-cutting due to the constant leaps in the cable bill, the first thing that comes to our minds is Netflix...

This 70-inch 4K TV is on sale for $499.99 in early Black Friday deal at Best Buy

Best Buy is releasing early Black Friday deals this year, and we've spotted a stellar bargain on a big-screen 4K TV. Today only, you...