Service Providers

Service providers are the central place for all Yuga application service registering.

Your own application, as well as all of Yuga's core services are registered via service providers. These include:

  • Elegant Database

  • View

  • Validation

  • Session

  • Events

  • Router

  • Console Application, to name but a few.

Registered Service Providers are found in config/ServiceProviders.php When the file is opened, you will see an array returned from that file which array contains all the registered service providers, When you create a service provider using a yuga command, this is where it's registered, you can also insert it there by hand if you will.

All service providers extend the Yuga\Providers\ServiceProvider class and they all contain a load method, other methods may be introduce in future and we'll let you know. Use the load method to bind everything you want to use in your application.

The Yuga command used to generate a service provider is as below:

php yuga make:provider LogServiceProvider

It's within the load method that things are bound to your application, "things" in this case could mean classes that you want your application to have on every single request or strings, it could pretty much be anything.

The following code represents a basic service provider and how the load method must be used. All services are bound to the $app variable provided as an argument to the load method as below:

<?php

namespace App\Providers;

use App\Models\User;
use Yuga\Providers\ServiceProvider;
use Yuga\Interfaces\Application\Application;

class TestServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function load(Application $app)
    {
        $app->singleton('my-users', function ($app) {
            return User::all();
        });
    }
}

Inside of your controllers or view-models, you can get the service that has been already registered in the following way:

<?php

// for a controller

namespace App\Controllers;

use Yuga\Controllers\Controller as BaseController;

class UserController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function users($app)
    {
        $users = $app->make('my-users');
        
        // or 
        
        $users = $app['my-users'];
    }
}
<?php

//for a view-model

namespace App\ViewModels;

class Test extends App
{

    /**
     * Load or / manupulate data when its a get request
     */
    public function onGet()
    {
        $users = app()->make('my-users');
        
        // or 
        
        $users = app()['my-users');
    }
}

Last updated