SchweppesAle Posted December 27, 2009 Share Posted December 27, 2009 hi, just looking for some advice on how to choose appropriate controller names which adhere to the MVC pattern. I have this banner platform which I'm working on, and I felt that there where a bit too many controllers. Ex: I had one which allowed for modification of banners, one which created new banners, one which displayed a list of banners. This was in addition to controllers which manage clients, locations, etc. I felt like there where too many controllers though so I deleted the modify, list, create controllers and placed all functionality within the "banner" controller (1 huge switch statement - see below). /*fetches the appropriate task so we know which function within the view to execute*/ $task = JRequest::getVar('task', 'listBanners'); $MV = parent::setMV(); $model = $MV['model']; $view = $MV['view']; /*Fetches required Variables*/ $id = JRequest::getInt('id', NULL); $Title = JRequest::getVar('Title', NULL); $bannerImage = JRequest::getVar('bannerImage', NULL); $destination = JRequest::getVar('destination', NULL); $locations = JRequest::getVar('locations', NULL); $startDate = JRequest::getVar('startDate', NULL); $endDate = JRequest::getVar('endDate', NULL); $state = JRequest::getVar('state', NULL); switch($task) { /*lists all available banners*/ case 'listBanners': $Banners = $model->getBanners(); $view->listBanners($Banners); break; /*use list to unpublish a banner*/ case 'unpublish': $model->unpublish($id); header('Location: index.php?option=com_jtbanners&view=banners'); break; /*use list to publish a banner*/ case 'publish': $model->publish($id); header('Location: index.php?option=com_jtbanners&view=banners'); break; /*displays options for each banner after clicking via list*/ case 'clickBanner': $bannerImg = $model->getBannerImg($id); $view->bannerOptions($bannerImg); break; /*allows user to modify any of banner's settings*/ case 'updateBanner': $bannerData = $model->getBannerData($id); $locations = $model->getLocations(); $bannerImages = $model->getBannerImages(); $view->updateBanner($bannerData, $locations, $bannerImages); break; /*saves changes user has made to a banner*/ case 'update': $model->updateBanner($id, $Title, $bannerImage, $destination, $locations, $startDate, $endDate, $state); header('Location:index.php?option=com_jtbanners&view=banners'); break; /*dispays form, allowing user to create a new banner*/ case 'createBanner': $locations = $model->getLocations(); $bannerImages = $model->getBannerImages(); $view->createBanner($locations, $bannerImages);[sup][/sup] break; /*Inserts banner into database*/ case 'insert': $model->newBanner($Title, $bannerImage, $destination, $locations, $startDate, $endDate, $state); header('Location: index.php?option=com_jtbanners&view=banners'); break; default: $Banners = $model->getBanners(); $view->listBanners($Banners); break; } } In summary, is it best to name your controllers after verbs which describe their functionality or as in this case, nouns? Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted December 27, 2009 Author Share Posted December 27, 2009 sorry if the answer is obvious, I'm trying to do it right though Quote Link to comment Share on other sites More sharing options...
ignace Posted December 27, 2009 Share Posted December 27, 2009 We generally adhere to plurals (UsersController in combination with .htaccess you can create users/ignace) Now back to MVC. You can create one controller for each page in your website or create one controller that handles multiple related actions: class EditPageController extends Controller { public function __construct(.. } //or class PagesController extends Controller { public function addAction() {} public function editAction() {} } A good practice is to keep your controllers thin and your models fat. By this they mean that your controller should have nearly no logic while your models should have it all. This approach ensures software flexibility for example: class UsersController extends Controller { public function addAction() { if (UserRepository::add($this->getRequest())) { $this->getResponse()->redirect('/users'); } } } As you can see above my code knows nothing they don't know what sort of request they got for all they know I could have done something like: > index.php --controller users --action add --username ignace --email ignace@mail.com or POST http://domain.com/users/add username ignace email ignace@mail.com or .. The best thing you can do is write your software in such a way so that everyone knows only what they need to know and nothing more the same goes for your website. Depending on what sort of request you get your software may return plain/JSON/XML/HTML/XHTML I list HTML and XHTML separatly because through content negotiation you can decide wether the client browser supports XHTML. The same goes for language an american gets everything in english while a frenchmen gets everything in french while your application logic remains mostly the same for all requests (http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html). Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted December 27, 2009 Share Posted December 27, 2009 I "normally" create one controller for each "type" of things then put different functions to handle the actions. So Users would have it's own controller. Then inside the users controller there would be a function: login, logout, forgotpassword, and everything else. Those deal with the login page, logout page and so forth. This is generally how I set up my controller. There are even times when there are a lot of misc things I need but there so small they may not need a controller I use on called misc controller. That's what I do. I use the same naming convention in Codeingitor, cakephp, zend, and the others. Almost anything that deals in MVC, that's how I name them. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted December 28, 2009 Author Share Posted December 28, 2009 *scratches head* So does that mean that I added too much logic to my controllers? I figured that you use the controllers to assign appropriate models, make appropriate calls to the model then send that data over to the view. That's why each one has a switch statement. Should I rewrite this? I'm trying to get it ready for an interview, that's why I'm kind of stressing the design right now Quote Link to comment Share on other sites More sharing options...
ignace Posted January 1, 2010 Share Posted January 1, 2010 Probably to late but yeah I would rewrite it interview or not IMO that's a bad approach and will not get you the job. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.