Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

laravel - what is the best way to move logic from controller to a service

my question is : How to take my business logic out of controller and transfer it to a service ??? I am currently using two separate controllers for Post model in Laravel. One for user-related logic and one for administrator-related logic. pseudo code for AdminPostController :

class AdminPostController
{
    public function index(MaybyIndexRequest $request)
    {
        $something = do lines of  calculation
        return return PostResource::collection($something);
    }

    public function storeAdminPost(StorePostRequest $request)
    {
        $something = do lines of  calculation
        return return PostStoreResource::collection($something);
    }
}

pseudo code for UserPostController :

class UserPostController
{
    public function maybyUserindex(AnotherIndexRequest $request)
    {
        $something = do lines of  calculation
        return return UserPostResource::collection($something);
    }

    public function storeUserPost(OtherStorePostRequest $request)
    {
        $something = do lines of  calculation
        return return UserPostStoreResource::collection($something);
    }
}

I want to transfer the business logic of these two controllers to another class and call them with the help of, for example, a Facade like : class AdminPostController { public function index(MaybyIndexRequest $request) { $something = PostService::($request); return return PostResource::collection($something); }

    public function storeUserPost(StorePostRequest $request)
    {
        $something = PostService::Store($request);
        return return PostStoreResource::collection($something);
    }
}

But I do not know with what design patterns I should do this. Or what I'm looking for is not a good way to get the code out of the controller !!! The way to solve this problem came to my mind : factory pattern : a class that has two methods called user() and admin().

class PostFactory
{
    public function AdminCommands()
    {
        return new AdminPostCommands(); // a class that contains admin 
    }

    public function UserCommands()
    {
        return new UserPostCommands(); // a class that contains user related logics

    }
}

That the user method returns an instance of UserPostCommands class (including the user's logic) and the AdminCommands class method (contains the's post logic) .... or :

    class PostFactory
    {
            public function Factory(User $user)
        {
            if ($user->isAdmin){
                 return new AdminPostCommands(); // a class that contains admin 
            }else{
                 return new UserPostCommands(); // a class that contains user related logics
            }
    
    }

a class that it takes an instance of the authorized user to decide whether the AdminPostCommands OR UserPostCommands class should be Returned. each of these two classes(AdminPostCommands or UserPostCommands ) has different methods. For example, the user may not be able to delete a post . Of course, user-related methods will only be used in the user controller and vice versa.

question from:https://stackoverflow.com/questions/65936662/what-is-the-best-way-to-move-logic-from-controller-to-a-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look's like you are deviating from the standard conventions of Laravel.

Perhaps, it might be worthwhile spending some time learning about them. For example, https://laravel.com/docs/8.x/controllers#resource-controllers on how to structure controllers.

storeUserPost should simply be store, you are in the UserPost controller, so it's implied that what you are storing will be a UserPost. Any logic required can be moved to an event or a service/utility class.

https://laravel.com/docs/8.x/events#introduction

A controller shouldn't, usually, be doing more than querying/storing/updating the model with the new data or calling other classes that perform logic and returning a view or redirect, generally speaking.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...