lizeshakya's avatar

What is your Laravel design pattern?

For me, it is being very difficult to implement SOLID design principle in Laravel application. It gives ache to refactor and implement new feature in the code. How do you achieve SOLID principle in the existing project?

0 likes
5 replies
Braunson's avatar

In existing projects you'll have to plan a way to move things part by part or module by module. It'll be tedious in an existing project, especially when it's a larger project.

A good Laracasts series is https://laracasts.com/series/solid-principles-in-php if your looking to learn more SOLID principles in PHP.

I'd suggest if you are trying to use SOLID to stick to it as much as possible but no code is perfect and you'll likely not be able to stick to it 100% for every piece of code so don't sweat the small things ;-)

1 like
martinbean's avatar

@lizeshakya I’m not sure what you mean by “design pattern” in your context. If you mean approach to Laravel projects, then I take a “DDD-lite” approach. I group models into directories based on the “part” of the application they’re in, and then create resource controllers around those models. Validation is put in form request classes and authorisation is handled by policies. Sticking to these conventions leads to slim controllers and easy-to-manage applications for me.

For me, it is being very difficult to implement SOLID design principle in Laravel application.

SOLID is just a way of approaching code. Laravel is very much open to using solid principles if you choose to.

How do you achieve SOLID principle in the existing project?

Well, that’s a difficult question to answer because it doesn’t have one, single answer. It depends on the project and what you’re trying to do with it. No one could possibly tell you how to implement SOLID in a project without knowing anything about the project itself or the current state of that project’s code.

My rough approach would be to whip models into shape. Models are the very core of your application; they are what your application is. If you have “bad” models, then all code you write from there is going to suffer. Define your models, and then go from there.

I’d then look at re-factoring any bad practices. Re-factor things to interfaces. Inject dependencies in classes. Bind things to the container. DRY out any code repeated in the codebase. Move logic from controllers to models. This steps should lead to a leaner, easier-to-manage codebase.

1 like
lizeshakya's avatar

@BRAUNSON - Thank you for the response. Yes I have viewed the solid principles in PHP and it is one of the best series in the Laracasts. Will try to use SOLID wherever it is possible and revisit the SOLID series and this thread when I am unable to :)

Please or to participate in this conversation.