'Business controller' shouldn't access Model DAO... it should initiate the model it and pass it to the view.The view then accesses the model to request the result. The controller shouldn't be doing anything to do with business logic (e.g. fetching a result) This should be done in the model after it's been passed to the view. In some cases the controller may set options on the view, but it shouldn't directly govern how the model and the view interact.
just because you're calling a function on the model from the controller doesn't mean you've moved the business logic out of the controller.
A good example is pagination. If your controller is doing this:
PHP Code:
$perPage = 10;
$page = $_GET['page'];
$users = $this->user->find('lastlogin < "2009-05-10"', $perPage, $perPage * $page);
$totalPages = $this->user->count('lastlogin < "2009-05-10"') / $perPage;
$this->view->assign('users', $users);
$this->view->assign('totalPages', $totalPages);
$this->view->assign('page', $page);
You have business/display logic in the controller and should be doing something more like this:
PHP Code:
$this->view->assign('user', $this->user);
$this->view->assign('filter', 'lastlogin < "2009-05-10"');
$this->view->assign('page', $_GET['page']);
For several reasons:
- You've removed the business logic from the controller
- The view can now be much more easily re-used, without having to manually work out the options each time.
- Now that the view has access to the model, you have much better control over what the view is doing. Perhaps the view should always add some extra filters, e.g. only users which aren't deleted. Otherwise these extra filters needs to be added in each controller. Perhaps it has different filters based on the current user level? This is all display logic and should be in the view--not the controller.
- The view controls its own error conditions, e.g. it's easy to implement "Sorry, no records were returned but here's some you may be interested in" because it can ask the model for something else. Otherwise you need this logic in the controller too.
Bookmarks