thesimons's avatar

Is it normal to get lost in your own Laravel project?

Hey everyone,

This isn’t really a help request — more of a curiosity.

I’m currently working on my first big project with Laravel, and sometimes I feel like I get a bit lost inside my own app — jumping between controllers, structures, and so on.

Is that something everyone goes through at some point, or does it mean I’m doing something wrong in how I’m structuring things?

Thanks, Simon

0 likes
11 replies
jlrdw's avatar

If you watch @jeffreyway code many times he will put in what's needed to get things working. Then he will refactor the code as needed. Like put some code in a special class.

Just view some videos here and you will get the idea.

Also writing out what's needed with pencil and paper helps me.

Also while coding don't forget about validation, authorization, and authentication.

thesimons's avatar

Yes, I'm doing the same. Today I refactored a controller with over 500 lines of code. Split it in several controllers etc etc.

I will definitely with the videos you mentioned.

Thanks!

jlrdw's avatar

I like lean controllers, so an example:

If I have a complex search to perform I will have a little class for that, and return to the controller.

june23's avatar

You should look into Service classes and the The Single Responsibility Principle from SOLID (if you don't know SOLID, I would use an favorite AI assistant like Gemini to help you understand it and Service classes), they isolate the business logic from the Controller classes so that the Controller classes can do more of the "plumbing" like returning json() responses or doing Form Request Validations or status codes and calling the actual Service class within the Controller class. I think that can help breaking down the code that way you don't have a massive Controller class to deal with. Service classes help split this up. I would also look into Repository Classes to, they abstract the Database Operations (like findById(), etc...) some what like the Service classes isolate the business logic from a Controller.

Any way just thought that might help in managing lots of code.

jaseofspades88's avatar

Muscle memory helps and once you're comfortable with the layout of a codebase, you'll be fine. Get a good IDE and remember helpful keyboard shortcuts; specific file search and recently edited files are two of the most useful shortcuts you'll ever use.

1 like
Snapey's avatar

This is normal.

The only cure is doing more of it.

I imagine its the same as a mechanic and starting at a new garage, pulling out all the drawers to find the right tool. The only fix is to learn where things go.

Sorry.

martinbean's avatar

@thesimons Personally? Not really, given I’ve written each and every line of code in my own personal projects, and I stick to Laravel’s default conventions and best practices. That means I’ll seldom have controller actions with more than 5 lines of code, let alone 500 😬

It sounds like you might need to spend a bit more time thinking about your models, and extracting logic when it becomes a bit “hairy”. My controller actions usually either read or write an Eloquent model. If I have more complicated business logic, then I’ll extract it to a service or more appropriate class. I wouldn’t just keep adding and adding and adding code to a controller action so it was many hundreds of lines long.

For the application, try to spend a bit more time thinking around your entities and their relations. It may be you’re trying to meld domain concepts into existing models when there’s maybe a new model that needs unearthing.

An example I use often is when people are modelling “orders” in an e-commerce application, and start stuffing related concerns into that Order model: things like shipping when a separate shipment model can be extracted and used to express items from an order being shipped to a particular destination via a carrier. Instead of smashing those columns on your existing orders table, you’d instead create a separate shipments table with those shipment-related columns.

thesimons's avatar

My biggest problem for now is sticking with Laravel's default conventions.

Having coded for 22 years using core PHP and bit "indie" about coding. I really appreciate how things are organised under Laravel.

thesimons's avatar

Thanks for your terrific answers. I fell better knowing that I'm not alone.

With "big" I mean "large". I have created many other apps in Laravel before. Some of them really "mission critical". I have developed a full payment gateway handling 3DS etc etc. A CMS able to encoding videos and many other smaller things.

This is the first I face a so huge amount of data.

Thanks also for the advices :)

thesimons's avatar

I have fixed half of my issue moving from VSCode to PhpStorm.

I tried PhPStorm months ago without success. But this time, frustrated by inconsistency of VSCode I decided to force myself sticking to it.

Good choice. I'm "less lost".

Thanks, Simon

Please or to participate in this conversation.