So I wonder why Fortify (Fortify has several methods about login, logout, register, ...) doesn't have written its methods in a Service. I also notice that the Fortify actions don't have any execute() method, why ?
@vincent15000 You‘re asking a question that is basically about Taylor’s personal preference. I don’t know. Neither will any one else.
My best guess is Taylor created individual action classes so Fortify was easier to extend by end developers. If all the methods were in some random “auth service” class, then that would mean developers would need to extend that one class just for the sake of overriding one method.
Here is an example : I have a common CRUD controller GroupController and I need to add 2 more methods : addUserToGroup and removeUserFromGroup. Some people advise to not add these methods to the GroupController because this controller is primarily for simple CRUD methods. So my idea is, to better organize my code, to write these two methods in another place : Actions or Services.
This won’t solve anything. You’d still need a controller to actually invoke these methods. You’re probably being advised not to put them in your existing GroupController because those actions aren’t manipulating a group resource, but actually a relation (users in groups). Therefore, a nested resource controller would be more appropriate that has its own resourceful methods:
-
GroupUserController::store (adds a user to a group)
-
GroupUserController::destroy (remove a user from a group)
do I have to write another controller (for example SubscriptionsController) which will have more business logic and will for example have an add and a remove method and from these methods I call the actions or the services ? Or is it better to add these methods directly in the GroupController and call the actions / services from there ?
What has subscriptions got to do with users and groups…?
All actions and services are, are classes and methods to store business logic. They‘re just places you would lift business logic out of a controller, and in to make reusable. All it is, is instead of this:
public function someControllerAction()
{
// Some horribly long business logic
// That spans many, many lines of code
// And makes your controller actions look ugly
// And the code unusable in other contexts
// Such as Artisan commands or queued jobs
// Return response
}
Is this:
class SomeActionClass
{
public function execute()
{
// The horribly long business logic from the controller
// That can be executed without any knowledge of request or response logic
}
}
Or this:
class SomeServiceClass
{
public function someServiceMethod()
{
// The horribly long business logic from the controller
}
public function someOtherServiceMethod()
{
// Another method containing business logic around a particular entity
}
public function yetAnotherServiceMethod()
{
// More business logic around a particular entity
}
}