I have a simple web application with database tables orders and products that I’m reworking with CakePHP. There are more, but for simplicity, I’m using just these two here. In the web app, I’d like to be able to search items, search orders, and do order-related jobs, but I’m having difficulty setting up and choosing components to use to make this web app. As an example to my problem, I have the following page. Check out the layout below.
|------------------------------|
| My App |
|------------------------------|
|Search <--| search order |
|Orders | search product |
|etc. | |
| | |
| | |
|------------------------------|
Figure: left navigation bar and body shown. Arrow denotes page we’re on.
-
Controllers: for this search page, which controllers should be used? I’m defaulting to
PagesControllerfor static pages right now, but should this search page really usePagesController? I also haveProductsControllerandOrdersControllerthat were created when I createdProductandOrdermodels, but it seems more appropriate to use a completely new controller, perhaps one calledSearchController. This way the URL stays undermyapp/search/...and doesn’t go all over the place. Is this the proper way? (I know I can rewrite URLs, but that’s for later) -
Models and actions: I have
ProductandOrdermodels. Each of these models have a search logic for its respective DB table. If a new controller was created for the above page, then that controller can simply use these models’ actions right? I’m reading controller and model separation is completely normal and actually a good thing. At first I thought they went together, but the more I familiarize with CakePHP, it seems to be the opposite. -
If my approach (from 1 and 2) is not inefficient, wrong, or improper, could you suggest a good structure to set this specific page up with respect to what controllers to use and how the controller should be using models and their actions?
TLDR:
The real answer is – personal preference. It’s not a big deal either way whether you create a
SearchesControlleror use theProductsControllerandOrdersController(but don’t use PagesController).My preference and reasoning
I would create a
SearchesController. This just makes the most sense for organizational purposes. All your search functionality can be in the same controller, all your search views will be in the “Views/Searches/” folder, and any/all related css and javascript can be in/css/searchesor/js/searches/(if you like to organize that way like I do).You’ll also want to make a Search model (
Models/Search.php) and within it, add:public $useTable = false;[details] to tell it you aren’t going to make asearchestable.Within each of the SearchController’s actions like
function orders()andfunction products(), you can use$this->loadModel('Order');[details], to make those models accessible from theSearchesController, then run your searches.To stick with the “Fat Models, Skinny Controllers” mantra, I recommend keeping your controller code small – something like this:
Then, in your Order model (per this example), do the meat of the logic (below is rough idea):
Keeping all your controller actions together allows you to more easily make sweeping changes – like if you want to set a single variable for results per page that changes all your search pages…etc. I’m sure there are a lot of other ideas, but bottom line, it’s keeping similar code together.