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

Drewlim7's avatar

How to create a class or service and call the method in specific controller?

I am using the database notification method from laravel and I have to create the message content based on the notification class. However, I have 3-4 controllers which is using the same function to generate message content. Therefore, I am planning to abstract it to act as a service but I have no idea where to start as I saw some of the StackOverflow stated to use trait and some suggested using class. Currently, I am putting all the functions in a controller but I saw some stated that calling a function from another controller is a bad structure and might have a hidden problem. May I know which is the best way to handle the following scenario?

  1. Generate message content based on the notification class
  2. Show all notifications based on user_id, In this case, I might need to include the authorization part because I only need to fetch all the notification based on user_id
  3. Able to perform mark as read task and this also needs to have the authorization part to mark the correct data with correct user_id.
0 likes
2 replies
MortenS's avatar
MortenS
Best Answer
Level 7

I would create a class somewhere called NotificationService with methods that handle the scenarios you mentioned. This service can then be used wherever you need it, like in controllers, actions, queued jobs, or event listeners. If a Controller needs to use the service, you can just add it to the constructor (Dependency Injection) and Laravel will take care of the rest.

class SomeController extends Controller
{
	private $notificationService;

 	public function __contruct(NotificationService $notificationService)
	{
		$this->notificationService = $notificationService;
	}

	public function someMethod(Request $request)
	{
		// Use the service
	}
}

In some cases, like when you want to be able to switch between different "versions" of the same Service (meaning the perform the same tasks, but do it in different ways), it's a good idea to create a common Interface, and inject that instead of a concrete class. This way, the "consumer" - in this case a controller - doesn't really know or care which "version" of the service it's talking to. I only mention this because you'll probably come across it a lot when you're googling about the Service Pattern, and it can be confusing and make things unnecessarily complicated. Keep it simple, and just create a single class and start injecting that. You can always refactor later if the need arises.

Drewlim7's avatar

@mortens Thank you for your explanation. Instead of doing that way, could I do something like this ?


private $notificationService;

public function __construct()
    {
        $this->middleware(['auth', 'verified']);
        $this->notificationService = new NotificationService();
    }

Please or to participate in this conversation.