CodeIgniter Forums
PageController for GETs + DataPointController for POSTs? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: PageController for GETs + DataPointController for POSTs? (/showthread.php?tid=91253)



PageController for GETs + DataPointController for POSTs? - sillyquota - 07-09-2024

I'm new CI and the MVC pattern. I'm making a simple app to track my nicotine intake by storing usage into a database and displaying that data in the form of charts (using Chart.js)

Without getting too detailed, I have this in my app/Config/Routes.php:

PHP Code:
$routes->get('/new''PageController::newDataForm', ['filter' => ['authfilter''csrf']]);
$routes->post('/new''DataPointController::newDataEndpoint', ['filter' => ['authfilter''csrf']]);
// eg: GET   PageController::updateDataForm
//     POST  DataPointController:updateDataEndpoint
//     GET   PageController::deleteDataForm
//     POST  DataPointController:deleteDataEndpoint
// $routes->post('/api/new', 'APIController::newDataEndpoint'); // TODO: will be used for version 2 of the app: a Svelte/Solid.js/etc frontend which makes RESTful calls to CI API endpoints 


I currently house my HTML forms inside PageController as xxxxxxxForm(). The forms POST to DataPointController::xxxxxxEndpoint(), where it interacts with a DataPointModel to add the data into the DB.


Is the above practice considered abnormal or bad? Should I rewrite the code to have all my HTML-related code and Database-inserting-code housed within PageController (and get rid of DataPointController)? 
When I originally created this app, it seemed to make sense to me because the HTML forms are all in a "PageController" and the database stuff is all in a "DataPointController".... but now I'm not so sure. The problem I perceive now is when invalid data gets POSTed, I have to `return` from DataPointController using `return (new PageController())->newDataForm()` instead of `$this->newDataForm()`.
I have a feeling that this is inefficient and/or using more memory than necessary since its creating a new instance of PageController from within DataPointController.

Thoughts? Recommendations?


RE: PageController for GETs + DataPointController for POSTs? - InsiteFX - 07-09-2024

This may help you understand MVC a little better.

Get to know MVC better and learn why it's so great.

Dev.to - PHP MVC: the easy way


RE: PageController for GETs + DataPointController for POSTs? - sillyquota - 07-10-2024

Thanks! I'm refactoring my code now Smile


RE: PageController for GETs + DataPointController for POSTs? - InsiteFX - 07-11-2024

Let me know how you make out with this.


RE: PageController for GETs + DataPointController for POSTs? - sillyquota - 07-11-2024

(07-11-2024, 03:32 AM)InsiteFX Wrote: Let me know how you make out with this.

Originally I was housing a lot of pages in the PageController, so I ended up significantly increasing the number of Controllers I had (1 for each page). As well as rename some of the Views to match the function-name in the Controller. Now the code feels a lot less cluttered Smile and more manageable.

I'm really impressed with how CI is built.. very extensible, easy to use, and extremely feature-rich! I'm hooked.


RE: PageController for GETs + DataPointController for POSTs? - InsiteFX - 07-11-2024

Glad things worked out for you.

I started way back with CodeIgniter version 1.6.x.