SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Controller to the View

    So I am working on making my own lil framework thingy, and I understand the controller(s) talking to the model(s):
    PHP Code:
     class Clients extends Controller {
      public function 
    getClients() {
       
    $c = new Clients_m();
       return 
    $c->read();
      }
     } 
    But I don't know the best way to integrate the communication between the controller and the view? Like can someone show me an example of how a page would generate the view, then grab the data from the controller?

    Like this would be my inital idea:
    PHP Code:
              include_once('app/lib/controllers/Controller.php');

              
    $c = new Clients();
              
    $a = new Addresses();
              
    $v = new View(TEMPLATE_DIR.'test.tpl');

              if(
    $c->user->canDo('aclients')) {
                  try {
                      
    $clients $c->getClients();
                      
    $i 0;
                      foreach(
    $clients as $client) {
                          
    $newClients[$i] = $client;
                          
    $newClients[$i]['address'] = $a->getAddresses($client['ID_addresses']);
                          
    $i++;
                      }
                      
    $clients $newClients;
                      
    $v->assignVariable('clients',$clients);
                      
    $v->render();
                  } catch (
    Exception $e) {
                      echo 
    $e->getMessage();
                  }
              } else {
                  
    $v->assignVariable('clients','You are not authorized to view this.');
              } 
    Thanks!

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The model is a passive component. It represents the domain of the application. You could say that it models the real world, which is where it takes it name. In your example above, classes such as Clients and Addresses appear to be genuine model components.

    The controller is the part of your application which 1) updates the model and 2) decides which view to use. The controller does not need to be an object - it may take the form of a procedural script. In the simplest form, the controller will be the script which is directly addressed by the request URI. As a rule of thumb, the controller is the part of your code which decides how to handle a request.

    The view is responsible for rendering the output. In your example above, the view and controller seem to be mixed in the second code-block presented. This is quite normal - the concept of a page is a mix of view+controller into one entity.
    The class that you have named View appears to be a template. A template is part of the view, but is not the only thing dealing with view responsibility. The part of your code which decides which model to use, and which pulls data from those model components are part of the view as well. You do not need to use templates in your view either - It's just one of several strategies.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •