Jump to content

Better understanding of MVC's don't want to use real working ones just the idea


j.smith1981

Recommended Posts

Hi there

 

I am never the one to use something I don't simply understand but I was wondering if you could help me through why an errors occuring with my first attempt at a very primative MVC.

 

Ok I understand them as being somewhat Model (data whether that be a database or a file system it's just data!), View is the way the page is assembled in (ZF that would be also the Layout right?) and then finally Controller (that handles all the user known and unknown requests), the controller is responsible for sending any responses back to the user, would you ever have view take the data from the model or would it always go via the controller? I know it wouldn't get output to the user as they may never understand it in it's raw form.

 

But this is what I have come up with from this tutorial here: http://php-html.net/tutorials/model-view-controller-in-php/

 

This is what I have done in I think completion:

 

index.php:

<?php

// NOT TO BE SHOWN IN A PRODUCTIONAL APPLICATION ALWAYS USE FALSE WHEN IN A PRODUCTIONAL SERVER!
ini_set('display_errors', true);

// We require the class for this to work so we do so using the require_once construct:
require_once 'Controller.php';

// We ask for an object to be created of class/blueprint Controller but with no parameters
$controller = new Controller();

// We then make invoke action as a function within Controller class
$controller->invoke();

 

Controller.php:

<?php

require_once 'Model.php';

class Controller {
  
  public $model;
  
  public function __construct()
  {
    
$this->model = new Model();

  }
  
  
  public function invoke() {
  
    if(!IsSet($_GET['book']))
{

  $books = $this->model->getBookList();
  require 'booklist.php';
  
} else {
  
  $book = $this->model->getBook($_GET['book']);
  include 'viewbook.php'; // we could actually include this as one file though right?
  
}

  }

}

 

Model.php:

<?php
require_once 'Book.php';

class Model {

  public function getBookList()
  {
  
    return array('Jez\'s Great Book!'=> new Book('Jez\'s Great Book!', 'The best book on the planet about MVC\'s'),
             'PHP5 and MySQL Bible'=> new Book('My most enterprising PHP and dynamic page content book ever!'));
  
  }
  
  public function getBook($title)
  {
    $allbooks = $this->getBookList();
return $allbooks[$title];
  }
}

 

Book.php:

<?php

class Book {

  public $title;
  public $author;
  
  public function __construct($title='', $author='')
  {
    $this->title = $title;
$this->author = $author;
  }
}

 

booklist.php:

<?php

foreach($books as $title=>$book){
  echo "<pre>";
  echo '<a href="index.php?book='.$book->title.'">$book->title</a>';
  echo "</pre>";
  
}

 

viewbook.php

<?php

echo $book->title;
echo $book->author;

 

It's not the same as the tutorial exactly but I just wanted to see if I could get it working, the only problem is (I mean my sample data's just for the fun of it), however when I go to see the booklist.php

 

The problem I am having is how to understand it getting the values into the list, when it says the 2nd one's basically not an index, does this come up for yourselves?

 

I am just a bit confused, happy though kind of got this working, thank you in advance,

Jeremy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.