Welcome Guest, Not a member yet? Register   Sign In
PageController for GETs + DataPointController for POSTs?
#1

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?
Reply
#2

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks! I'm refactoring my code now Smile
Reply
#4

Let me know how you make out with this.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5
Thumbs Up 

(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.
Reply
#6

Glad things worked out for you.

I started way back with CodeIgniter version 1.6.x.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB