zoldic
192
2
Laravel

What is the purpose of using & registering Service Providers?

Posted 1 year ago by zoldic

Hi guys, I'm kinda confuse with Service Providers, I have no idea what is the purpose of using & registering Service Providers (yes, I have read the laravel's doc but I still don't quite get it), it is for calling a class with its method easier

I know how to register class to aliases in config/App.php, and it help you to easier calling the custom class with its method (you don't need to keep importing it with "use namespace" on top of your controller file)

for eg. I make this custom class name called "dump.php" and put it inside new folder called "Customs" (App\Customs\Dump.php)

namespace App\Customs;

class Dump
{
    public static function diedump($var)
    {
        dd($var);
    }
}

register it inside aliases in the config\app.php

'aliases' => [
    ...
    'Dump' => App\Customs\Dump::class,
],

so if I want to use it on one of my controller file, I can simply use the '' to call the class's method (Dump's diedump method)

//use App\Customs\Dump.php;  //don't need to import this anymore

class ThreadController extends Controller
{
    public function index() {
        $var = "dumpthis";
        \Dump::diedump($var);
    }
}

So for the Service Providers, what's the point of registering it? any difference with the aliases?

Can you guys give me simple example & explanatin about it?

Thanks

Please sign in or create an account to participate in this conversation.