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

orest's avatar
Level 13

laravel controller methods naming convention alternatives

I have a FollowController and i want to

  1. get list of user that a user follows ( following )
  2. get list of users that a user is followed by ( followers )

However i can't do that in the same controller without breaking the naming convention or without introducing if statements in the controller.

I would like to know your opinion on this.

First Approach

Create separate controllers

class FollowersController extends Controller
{
	public function index(User $user)
	{
		return $user->followedBy;
	}

}

class FollowingController extends Controller
{
	public function index(User $user)
	{
		return $user->follows;
	}

}

Second Approach

Break the naming convention and fit both in the same controller

class FollowController extends Controller
{
	public function followedBy(User $user)
	{
		return $user->followedBy;
	}

	public function follows(User $user)
	{
		return $user->follows;
	}

}

Third Approach

Use a flag to decide whether to return either one or both ( in this case 1 extra request is avoided and you serve both in a single request )

class FollowControllerr extends Controller
{
	public function index(User $user)
	{
		if (request()->boolean('follows'))
		{
			return $user->follows;
		}
		elseif (request()->boolean('followedBy'))
		{
			return $user->followedBy;
		}
		return [
			'follows' => $user->follows, 
			'folllowedBy' => $user->followedBy
 ];
	}

}
0 likes
2 replies
Sinnbeck's avatar

I would personally go with the first one, as it follows the Single responsibility principle, and it uses crud principles

1 like
martinbean's avatar

@orest The first approach. “Following” and “follower” are concepts in your application, even though they opposite on the same model, so therefore it’s acceptable for them to have their own controllers.

Stay away from “non-CRUD” methods as much as you can, and definitelt stay away from lots of conditionals like in your third example.

1 like

Please or to participate in this conversation.