简体   繁体   中英

Laravel controllers and models structure

So basically, I have this project I'm planning. A kind of "social network". I was just wondering, as for now, I have a single controller (ProfilesController) wich takes care of status updating, profile editing, gallery uploading / viewing, settings etc.

Is this a good structure? Or should I separate it into different controllers like (ProfilesController, GalleryController, SettingsController, GuestbookController)?

And my models, I currently have Guestbook, PhotoAlbum, Profile and User models. Is this good? Or should some of theese maybe be merged into User or Profile?

Thanks in advance.

I think to keep things as REST ful as possible you should have separate controllers for the separate models. Your models should definitely each be their own files/classes.

1. Following the principle "single responsibility" okay that you create a controller for each process.

2. Laravel each table (that you used in the project) of the database must have a Model.

3. If you want to do good things would recommend to follow the principle "first API": Designing the structure of your API and then begin to program it. Having done this, recently begin to develop interfaces and your processes using your API.

I would stay with REST and really create For every table, a model and a controller. So you can develop your application in a very clean way.

If your "social network" or whatever application you want to develop will get bigger and more complex, this is the best way to keep it clean and clearly represented.

Once I started a small Project and was not really caring about following this principle, but you will get to the point you regret it.

So start your Project with a clean structure and create for every Model a Controller. I recommend to also use the Route::Ressource for defining the basic routes. So you follow all the conventions.

Route::resource('Model', 'ModelController');

For more informations, check the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM