Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

3000's avatar
Level 1

Lumen service provider not working?

Hi,

I'm new to Lumen, and just set up a fresh install according to the instructions on the website. I'm trying to create a service that i can use to put code in, that does not belong in a controller. a helper/service/utility class have you. But I keep getting error message:

"Target class [App\Prodivers\BatmanServiceProvider] does not exist."

This is what I have:

  1. I added this to my /bootstrap/app. php $app->register(App\Providers\BatmanServiceProvider::class);

  2. This i my service in /app/Providers/BatmanServiceProvider. php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class BatmanServiceProvider extends ServiceProvider {

public function register()
{
    return "batman!";
}

}

  1. and this is my controller app/Http/Controllers/MainController. php

namespace App\Http\Controllers;

use App\Prodivers\BatmanServiceProvider;

class MainController extends Controller{

public function __construct(BatmanServiceProvider $BatmanServiceProvider){

}

public function main(){
    print "hello space!";
}

}

What am I doing wrong? I want to be able to, from my controller, call my service, have it do some work that doesn't belong in the controller, nor in a database model.

grateful for any help!

0 likes
6 replies
martinbean's avatar
Level 80

@3000 You don’t understand service providers. That’s not how they work at all.

A service provider registers services in the service container. That’s what the register method is for: for registering things. It’s not a service itself that you return something from and type-hint in your application’s classes.

Your BatmanServiceProvider should instead register something in the container. So if you want a Batman service class, that’s what you’d register:

class BatmanServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->register(Batman::class, function () {
            // Build and return Batman service class instance...
        });
    }
}

Now, you’d type-hint the Batman service; not the service provider:

class BatmanController extends Controller
{
    protected $batman;

    public function __construct(Batman $batman)
    {
        $this->batman = $batman;
    }
}

Because you’re type-hinting the Batman service (and not the service provider), Lumen will look into the container, find the binding, and return that instance.

3000's avatar
Level 1

@martinbean thank you so much for this, i really appreciate it. i have now corrected my controller and serviice provider per your instructions. so, where would i put the actual batman service class?

martinbean's avatar

@3000 Where ever you want to, really. You could put it at app/Services/Batman.php

3000's avatar
Level 1

just did. thank you for your help!

1 like
3000's avatar
Level 1

@martinbean Thank you. Could you explain to me where/how I should structure my code then? Where, and how, do i put code that doesnt belong in the controller. say, code to generate an email template, or function to generate an uuid. stuff that i want to reuse in multiple controllers.

Please or to participate in this conversation.