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

maplerock's avatar

Split Controller Functionality

What would be the best way to acheive the following? Currently I have a checkout method within a controller, within this I process a stripe payment and then save the booking to the database. What would be the best way to split these two bits of functionality out into separate calls and where would I put the code behind them? Methods within same controller and call this $this or elsewhere?

So something like this?

function checkout(Request $request) {
doStripePayment();
doDbSave()
}
0 likes
1 reply
ChristopherSFSD's avatar

There's no "best" way. It all depends on the size and scope of your project.

One idea would be to create a "Service" class that is responsible for handling your business logic so that your controllers are nothing more than traffic cops.

For example: In my project I created a "Services" directory inside of the app directory. Then inside of that, I have an "Accounts" directory. When an account is being created, the AccountController calls the AccountCreator class and passes the input over to it. The AccountCreator is responsible for doing the work of creating an account and returning errors (if any) back to the controller which then either redirects with a success message or the errors.

1 like

Please or to participate in this conversation.