Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An example of an MVC controller

I have been reading a lot about how and why to use an MVC approach in an application. I have seen and understand examples of a Model, I have seen and understand examples of the View.... but I am STILL kind of fuzzy on the controller. I would really love to see a thorough enough example of a controller(s). (in PHP if possible, but any language will help)

Thank you.

PS: It would also be great if I could see an example of an index.php page, which decides which controller to use and how.

EDIT: I know what the job of the controller is, I just don't really understand how to accomplish this in OOP.

like image 926
BDuelz Avatar asked Nov 15 '09 15:11

BDuelz


People also ask

What is MVC and example?

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.

What is .NET MVC controller?

Controllers are essentially the central unit of your ASP.NET MVC application. It is the 1st recipient, which interacts with incoming HTTP Request. So, the controller decides which model will be selected, and then it takes the data from the model and passes the same to the respective view, after that view is rendered.

What are controllers and actions in MVC?

In ASP.NET MVC, a Controller is used to define and group a set of actions. An action (or action method ) is a method on a controller that handles incoming requests.


2 Answers

Request example

Put something like this in your index.php:

<?php  // Holds data like $baseUrl etc. include 'config.php';  $requestUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $requestString = substr($requestUrl, strlen($baseUrl));  $urlParams = explode('/', $requestString);  // TODO: Consider security (see comments) $controllerName = ucfirst(array_shift($urlParams)).'Controller'; $actionName = strtolower(array_shift($urlParams)).'Action';  // Here you should probably gather the rest as params  // Call the action $controller = new $controllerName; $controller->$actionName(); 

Really basic, but you get the idea... (I also didn't take care of loading the controller class, but I guess that can be done either via autoloading or you know how to do it.)

Simple controller example (controllers/login.php):

<?php      class LoginController {     function loginAction()     {         $username = $this->request->get('username');         $password = $this->request->get('password');          $this->loadModel('users');         if ($this->users->validate($username, $password))         {             $userData = $this->users->fetch($username);             AuthStorage::save($username, $userData);             $this->redirect('secret_area');         }         else         {             $this->view->message = 'Invalid login';             $this->view->render('error');         }     }      function logoutAction()     {         if (AuthStorage::logged())         {             AuthStorage::remove();             $this->redirect('index');         }         else         {             $this->view->message = 'You are not logged in.';             $this->view->render('error');         }     } } 

As you see, the controller takes care of the "flow" of the application - the so-called application logic. It does not take care about data storage and presentation. It rather gathers all the necessary data (depending on the current request) and assigns it to the view...

Note that this would not work with any framework I know, but I'm sure you know what the functions are supposed to do.

like image 55
Franz Avatar answered Sep 28 '22 10:09

Franz


Imagine three screens in a UI, a screen where a user enters some search criteria, a screen where a list of summaries of matching records is displayed and a screen where, once a record is selected it is displayed for editing. There will be some logic relating to the initial search on the lines of

if search criteria are matched by no records     redisplay criteria screen, with message saying "none found" else if search criteria are matched by exactly one record     display edit screen with chosen record else (we have lots of records)     display list screen with matching records 

Where should that logic go? Not in the view or model surely? Hence this is the job of the controller. The controller would also be responsible for taking the criteria and invoking the Model method for the search.

like image 22
djna Avatar answered Sep 28 '22 08:09

djna