postitief's avatar

Create own Service Provider

Hello all,

I hope you guys can help me out. In my Laravel 5 application, I need to log information of the visitor. On my search for something existing, I ran into this solution: https://github.com/antonioribeiro/tracker. It's a real nice peace of code, but way to "big" for my project. I probably use 5% of the whole package.

So since I also start learning Laravel, I thought, why not write my own. But i'm running stuck on that!

As far as I know know, I need to create my own ServiceProvider. There is some documentation about that on the Laravel website ( http://laravel.com/docs/5.0/providers ), and I watched this video ( https://laracasts.com/lessons/service-providers-decoded ). But i can't get started with this information. I understand the registering of the ServiceProviders, but I don't understand where I will perform my database query for storing the visitors information.

I'm not looking for a complete code, but some help to get started. What files do I need to create and on what location are they stored. Where will my logic be placed? Please don't shoot me, help me out with the a tutorial or a simple example.

Maybe I also need to get a look at this tutorial. http://www.codeanchor.net/blog/creating-custom-laravel-5-packages/

0 likes
4 replies
isaackearl's avatar

If you want to create a service provider then the way I like to think about it, is you are just creating a class you can use in alot of places (through dependency injection).

You should start by simply creating a php class, and making the functionality. Service providers come in handy when your class suddenly gets arguments it needs in the constructor. What is nice is you can create the service provider which allows you to tell the application what dependencies your class needs and get them for you.

You have the added advantage of being able to specify if you want the class to be a single instance that you use throughout the application, or if you want a new instance each time it is used.

If you have a specific question about something that doesn't make sense to you, then let us know and I'm sure someone will be able to answer.

Let me give you a practical example though so you have an idea of what you might do. Let's say I am creating an API, and I want to be able to send json responses.

I have a basic idea of the functionality I want to I create an interface called ApiServiceInterface and I put some functions in there. Then I create an ApiService that implements that Interface. I end up with some stuff like this:

public function respond($data, $headers = [])
{
    return response()->json($data, $this->getStatusCode(), $headers);
}

Anyway.. now I want to be able to use this in my controller... so I make a service provider file so I can do a bind.

$this->app->bind('Acme\Api\Services\Interfaces\ApiServiceInterface', function ($app) {
        return new ApiService();
});

Then I add this file to my app.php config file so the application knows to load it up.

'Acme\Api\Providers\ApiServiceProvider',

What this does is it tells laravel that anytime someone injects the ApiServiceInterface into a constructor or function etc, then to return a new ApiService. If you had dependencies for ApiService then you would specify those too... for example you would return this instead if for some reason your ApiService needed the request in the constructor.

return new ApiService(
        $app['Illuminate\Http\Request']
);

So now you can use it like this in your controller

protected $api;

function __construct(ApiServiceInterface $api)
{
    $this->api = $api;
}

and in your actions you can do something like this:

public function hello()
{
    return $this->api->respond(['message' => 'hello']);
}

Hopefully my explanation here makes some sense and is somewhat accurate. I simplified the explanations on many things because I just wanted to describe a practical usage rather then go into all the benefits and various ways you can implement things. Let us know if you have a more specific question.

8 likes
JarekTkaczyk's avatar

@postitief ServiceProvider is basically a place to bootstrap your package in the Laravel environment. Package is independent from the SP (if it's not only for Laravel, and that's something to aim for). ServiceProvider is kinda plug compatible with Laravel that lets the framework know of the package and prepares its functions, config etc.

postitief's avatar

Thanks guys, I will continue this in the coming week.

kureci's avatar

@isaackearl your post helped me a tonne! I had been reading the Laravel docs to understand where to exactly put the code I needed for the core service that my provider is meant to provide, but then the docs never mention anything about that.

Thank you!

Please or to participate in this conversation.