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

ruthback's avatar

Service classes in Laravel - code structure

Hi, sorry for the newbie question, I've actually read a ton of threads here and online trying to get a clear answer about this but...

I have a very big chunk of logic that I could easily refactor into its own service class, so that it can be invoked whenever is needed from a controller, job, command line, etc. But the question is where should I create this service class? I was hoping there would be an artisan command for this but the closest thing I found are Service Providers which doesn't look like what I need. There were a couple of suggestions online that said to create a file inside app/Services as well, and that makes sense to me but I'm not sure if this goes against Laravel's workflow?

All I really want is to be able to import my helper class from a controller or a queued job, and invoke some methods from it.

Thank you!

0 likes
6 replies
LaryAI's avatar
Level 58

In Laravel, there is no specific convention for where to create service classes. However, a common practice is to create a Services directory inside the app directory. You can create a new file inside the Services directory for your service class.

Here's an example of how you can structure your code:

  1. Create a Services directory inside the app directory if it doesn't already exist:
mkdir app/Services
  1. Create a new file for your service class inside the Services directory. For example, MyService.php:
<?php

namespace App\Services;

class MyService
{
    public function someMethod()
    {
        // Your logic here
    }
}
  1. In your controller or any other class where you want to use the service, import the service class and create an instance of it:
<?php

namespace App\Http\Controllers;

use App\Services\MyService;

class MyController extends Controller
{
    public function index()
    {
        $myService = new MyService();
        $myService->someMethod();
        
        // Rest of your code
    }
}

Remember to adjust the namespace and class names according to your project's structure.

By following this approach, you can easily organize your service classes and import them wherever needed.

1 like
ruthback's avatar

Ok so this somewhat answers my question... but just in case I'd like a human to confirm if possible :)

newbie360's avatar
<?php

namespace App\Services;

class MyService
{
    public function someMethod(): void
    {
        info('someMethod');
    }
}
<?php

namespace App\Http\Controllers;

use App\Services\MyService;

class MyController extends Controller
{
    public function index(MyService $myService): void
    {
        $myService->someMethod();
    }
}
2 likes
ruthback's avatar

Wouldn't it be better to use service containers in this case to make things simpler?

use App\Services\MyService;

class MyController extends Controller
{
	public function __construct(
		protected MyService $service
	) {}
}

But the question still is whether it's ok to place my service class inside the root app directory. The documentation has a page dedicated to folder structure but there's no mention of it being a good practice... https://laravel.com/docs/10.x/structure

Please or to participate in this conversation.