Laravel - do I need a service provider for each of my containers / user classes?

Service containers / suppliers are probably much simpler concepts than I can imagine, but after hours of reading I still don't get it completely.

I created a simple class DateFormatinside app/Library. After creating an alias for it inside, \config\app.phpI can use it right away in any controllers or blade templates.

<?php namespace App\Library;

class DateFormat {

    public static function getDate($timestamp){
      // processing the timestamp      
    }

}

Did I just create a services container? If so, do I also need to create a service provider? Where are the bindings to the picture?

I would really appreciate covering this topic. Thanks

+4
source share
1 answer

. . - Facade.

- .

, \My\Very\Long\Class\Adapter, config/app.php:

// config/app.php
<?php
'aliases' => [
    // a bunch of aliases
    'MyAdapter' => My\Very\Long\Class\Adapter::class,
]

:

<?php

new MyAdapter();
... 

:

<?php

use My\Very\Long\Class\Adapter;
...
new Adapter();
...

, , . , , , , . .

:

API, . SuperApi. SuperAPI , SuperApi - :

<?php
// Some method (a controller or something)
public function index()
{
    $superApi = new \SuperApi\Connector($key, $secret);
    return $superApi->getCustomers();
}

, , , ( , : $key $secret ).

Connector, , :

// config/app.php
<?php
'aliases' => [
    // a bunch of aliases
    'SuperApi' => SuperApi\Connector::class,
]

, :

<?php

// Some method (a controller or something)
public function index()
{
    $superApi = new SuperApi($key, $secret);
    return $superApi->getCustomers();
}

, $key $secret.

.

// app/Providers/SuperApiProvider.php
<?php

namespace App\Providers;

use SuperApi\Connector;
use Illuminate\Support\ServiceProvider;

class SuperApiProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('superApiConnector', function ($app) {
            return new ApiConnector($app['config']->get('super-api.key'), $app['config']->get('super-api.secret'));
        });
    }
}

// app/Providers/SuperApi.php (the Facade)
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Facade;

class SuperApi extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'superApiConnector';
    }
}


// config/super-api.config
<?php

return [
    'key' => env('SUPER_API_KEY'),
    'secret' => env('SUPER_API_SECRET'),
];

// config/app.php
<?php
'providers' => [
    // a bunch of providers
    App\Providers\SuperApiProvider::class,
]

, , ('superApiConnector'), , , - , , SuperApi.

, SuperApi\Connector, :

<?php

// Some method (a controller or something)
public function index()
{
    return SuperApi::getCustomers();
}

, , , Laravel IoC Container :

<?php
// Some method (a controller or something)
public function index(SuperApi $api)
{
    return $api->getCustomers();
}

, , . , . , , , .

+3

Source: https://habr.com/ru/post/1686068/


All Articles