freeza's avatar

Service Class. Did i understand it?

Hello guys, just a question about business logic.

First i put business logic like for example upload a profile image to user model in the user model. Then i noticed that i need it in my team model, too.

Now i created a ServiceClass "UploadFileService" for this business logic. Is this the right place?

My Models are cleaner now and the business logic is separated. I guess its ok so.

0 likes
8 replies
RomainLanz's avatar

Now i created a ServiceClass "UploadFileService" for this business logic. Is this the right place?

Totally.

Logic must not go into model, some people put logic into controller, but the best place is into service.

1 like
freeza's avatar

Hello @RomainLanz , but why i need an extra ServiceProvider? What is the benefit here? I could also create a normal Class "FileUpload". I dont understand the benefits of ServiceProviders.

RomainLanz's avatar

You don't need a ServiceProvider.
Service != ServiceProvider

I could also create a normal Class "FileUpload".

Yes, this is the way to go.

JeffreyWay's avatar

But I wouldn't add the "Service" extension, personally. There's no need for it. Just name the class what it does. "UploadsFile", if that's what you want.

2 likes
pmall's avatar

Logic must not go into model

Of course it does if the logic is model specific.

but why i need an extra ServiceProvider?

Service providers is for configuring and providing services, if needed. Lets say your UploadFile service needs to be injected some extra classes before being used (filesystem disk class for example), this configuration takes place in a service provider. If your service is a bare class that can be used like this, no need for service provider.

martinbean's avatar

Logic must not go into model

@RomainLanz Erm, that’s exactly where logic is supposed to go in an MVC-like application. You’re right in that it doesn’t belong in a controller, and it definitely doesn’t belong in a view.

Service classes are great for encapsulating logic that may be used in more than one model.

JeffreyWay's avatar

I think he was saying something along the lines of "That sort of logic shouldn't go in the model."

1 like
RomainLanz's avatar

I think he was saying something along the lines of "That sort of logic shouldn't go in the model."

Exactly!

Please or to participate in this conversation.