SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My attempt to MVC

    Im trying to get a hold of the whole MVC concept.
    This is my coding so far.

    Am i at the right track? What do i need to change and what is good with my coding?

    Thanx in advance, Crille

    Structure of the folders and files
    Code:
    .models
    ..guestbook
    ...guestbookModel.php
     
    .controllers
    ..guestbook
    ...guestbookController.php
     
    .views
    ..guestbook
    ...view.tpl
    ...post.tpl

    index.php
    Code:
    <?php
    include ('includes/config.php');
    
    if (isset($_GET['module'])) {
    	$module = $_GET['module'];
    	
    	if (isset($_GET['action'])) $action = $_GET['action'];
    	else $action = 'view';
    }
    
    else {
    	$module = 'default';
    	$action = 'view';
    }
    
    $model = 'models/' . $module . '/' . $module . 'Model.php';
    $controller = 'controllers/' . $module . '/' . $module . 'Controller.php';
    $template = 'views/' . $module . '/' . $action . '.tpl';
    
    if (!file_exists($model)) {
    	header ("Location: index.php");
    }
    
    if (!file_exists($controller)){
    	header ("Location: index.php");
    }
    
    if (!file_exists($template)){
    	header ("Location: index.php");
    }
    
    $modelClass = $module . 'Model';
    $controllerClass = $module . 'Controller';
    
    include $model;
    include $controller;
    
    $controller = new $controllerClass();
    $controller->setData("site", "model", $modelClass);
    $controller->setData("site", "template", $template);
    $controller->setData("site", "action", $action);
    $controller->prepare();
    $controller->render();
    ?>
    guestbookController.php
    Code:
    <?php
    class guestbookController {
    	private $registery = array();
    
    	public function __construct() {
    	}
    
    	/*
    	* ::Register set
    	* Sets a registery key
    	*/
    	public function setData($type, $key, $value) {
    		$this->registery[$type][$key] = $value;	
    	}
    	
    	public function getData($type, $key) {
    		return $this->registery[$type][$key];
    	}
    	
    	public function printErrors() {
    		if (!empty($this->registery["error"])) {
    			$error = "";
    			foreach ($this->registery["error"] as $key => $value) {
    				$error .= $value;
    			}
    			return $error;
    		}
    	}
    	
    	/*
    	* ::Validate form
    	* Checks if a form is valid
    	*/
    	public function formCheck($form) {
    		foreach ($form as $key => $value) {
    			if (!empty($value)) {
    				switch ($key) {
    					case 'email':
    					$validate = new validate;
    					if (!$validate->email($value)) $this->setData("error", "$value", "<p>$value är en felaktig E-Post!</p>");				
    					break;
    				
    					case 'url':
    					$validate = new validate;
    					if (!$validate->url($value)) $this->setData("error", "$key", "<p>$value är en felaktig URL</p>");
    				}
    			}
    			else $this->setData("error", "$key", "<p>$key måste fyllas i!</p>");
    		}
    	}
    	
    	/*
    	* ::Prepare
    	* Prepares actions for the page
    	*/
    	public function prepare() {
    		switch ($this->getData("site", "action")) {
    			case 'post':
    			if ($_POST)  {
    				$this->formCheck($_POST);
    				if (empty($this->registery["error"])) {
    					$headline = htmlspecialchars(str_replace("|", "", $_POST['headline']));
    					$name = htmlspecialchars(str_replace("|", "", $_POST['name']));
    					$email = $_POST['email'];
    					$text = htmlspecialchars(str_replace("|", "", $_POST['textarea']));
    					$time = date("Y:m:d");	
    					$instance = new guestbookModel;
    					$instance->write($headline, $name, $email, $text, $time);
    				}
    			}
    			break;
    			
    			case 'view':
    			$instance = new guestbookModel;
    			$this->setData("guestbook", "posts", $instance->read());
    			$guestbook = $this->getData("guestbook", "posts");
    			$lines = array_reverse(explode("\r\n", $guestbook));
    			foreach ($lines as $line) {
    				if (!empty($line)) $posts[] = explode("|", $line);
    			}
    			$this->setData("guestbook", "posts", $posts);
    			break;
    		}
    	}
    
    	/*
    	* ::Render
    	* Renders the page
    	*/
    	public function render() {
    		include $this->getData("site", "template");
    	}
    }
    ?>
    guestbookModell.php
    Code:
    <?php
    class guestbookModel {
    	private $file = 'guestbook.txt';
    	
    	/*
    	* ::Write
    	* Writes to the guestbook file
    	*/
    	public function write($headline, $name, $email, $text, $time) {
    		$content = file_get_contents($this->file);
    		$content .= "$headline|$name|$email|$text|$time" . "\r\n";
    		file_put_contents($this->file, $content);
    	}
    	
    	/*
    	* ::Read
    	* Reads from the guestbook file
    	*/
    	public function read() {
    		return file_get_contents($this->file);		
    	}
    }
    ?>
    view.tpl
    Code:
    <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    <?php
    foreach ($this->getData("guestbook", "posts") as $value) {
    ?>
    
    <p><strong>Rubrik</strong></p>
    <p><?php echo $value[0] ?></p>
    <p><strong>Av</strong></p>
    <p><?php echo $value[1] ?></p>
    <p><strong>E-Post</strong></p>
    <p><?php echo $value[2] ?></p>
    <p><strong>Meddelande</strong></p>
    <p><?php echo $value[3] ?></p>
    <p><strong>Skrivet</strong></p>
    <p><?php echo $value[4] ?></p>
    <hr />
    
    <?php
    }
    ?>
    <p><a href="index.php?module=guestbook">Visa gästboken</a></p>
    <p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a>
    post.tpl
    Code:
    <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    <?php 
    echo $this->printErrors();
    ?>
    <form method="post">
    <p><strong>Rubrik</strong></p>
    <p><input name="headline" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['headline'] ?>" size="30" /></p>
    <p><strong>Namn</strong></p>
    <p><input name="name" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['name'] ?>" size="30" /></p>
    <p><strong>E-Post</strong></p>
    <p><input name="email" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['email'] ?>" size="30" /></p>
    <p><strong>Text</strong></p>
    <p><textarea name="textarea" cols="50" rows="5"><?php if (isset($_POST['submit'])) echo $_POST['textarea'] ?></textarea></p>
    <p><input name="submit" type="submit" value="Skicka" /></p>
    </form>
    <hr />
    <p><a href="index.php?module=guestbook">Visa gästboken</a></p>
    <p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a></p>

  2. #2
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Comments from my side:
    1> Better make base class for Model, View & Controller.
    Extend your file as:

    class GuestbookController extends Controller{

    class GuestbookModel extend Model{

    2> Make class name in camecase structure with first letter capital.
    3> no need to use .tpl, if you want you can rather use .html.php
    4> $registery -> $registry btw
    5> user router class instead & handle the request mapping there.

    Better if you have wrapped the code with PHP type.

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    Comments from my side:
    1> Better make base class for Model, View & Controller.
    Extend your file as:

    class GuestbookController extends Controller{

    class GuestbookModel extend Model{

    2> Make class name in camecase structure with first letter capital.
    3> no need to use .tpl, if you want you can rather use .html.php
    4> $registery -> $registry btw
    5> user router class instead & handle the request mapping there.

    Better if you have wrapped the code with PHP type.
    Most of your comments are personal preferences that don't necessarily enhance the quality of what he's already written.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by t3ch View Post
    Im trying to get a hold of the whole MVC concept.
    This is my coding so far.

    Am i at the right track? What do i need to change and what is good with my coding?

    Thanx in advance, Crille

    Structure of the folders and files
    Code:
    .models
    ..guestbook
    ...guestbookModel.php
     
    .controllers
    ..guestbook
    ...guestbookController.php
     
    .views
    ..guestbook
    ...view.tpl
    ...post.tpl

    index.php
    Code:
    <?php
    include ('includes/config.php');
    
    if (isset($_GET['module'])) {
    	$module = $_GET['module'];
    	
    	if (isset($_GET['action'])) $action = $_GET['action'];
    	else $action = 'view';
    }
    
    else {
    	$module = 'default';
    	$action = 'view';
    }
    
    $model = 'models/' . $module . '/' . $module . 'Model.php';
    $controller = 'controllers/' . $module . '/' . $module . 'Controller.php';
    $template = 'views/' . $module . '/' . $action . '.tpl';
    
    if (!file_exists($model)) {
    	header ("Location: index.php");
    }
    
    if (!file_exists($controller)){
    	header ("Location: index.php");
    }
    
    if (!file_exists($template)){
    	header ("Location: index.php");
    }
    
    $modelClass = $module . 'Model';
    $controllerClass = $module . 'Controller';
    
    include $model;
    include $controller;
    
    $controller = new $controllerClass();
    $controller->setData("site", "model", $modelClass);
    $controller->setData("site", "template", $template);
    $controller->setData("site", "action", $action);
    $controller->prepare();
    $controller->render();
    ?>
    guestbookController.php
    Code:
    <?php
    class guestbookController {
    	private $registery = array();
    
    	public function __construct() {
    	}
    
    	/*
    	* ::Register set
    	* Sets a registery key
    	*/
    	public function setData($type, $key, $value) {
    		$this->registery[$type][$key] = $value;	
    	}
    	
    	public function getData($type, $key) {
    		return $this->registery[$type][$key];
    	}
    	
    	public function printErrors() {
    		if (!empty($this->registery["error"])) {
    			$error = "";
    			foreach ($this->registery["error"] as $key => $value) {
    				$error .= $value;
    			}
    			return $error;
    		}
    	}
    	
    	/*
    	* ::Validate form
    	* Checks if a form is valid
    	*/
    	public function formCheck($form) {
    		foreach ($form as $key => $value) {
    			if (!empty($value)) {
    				switch ($key) {
    					case 'email':
    					$validate = new validate;
    					if (!$validate->email($value)) $this->setData("error", "$value", "<p>$value är en felaktig E-Post!</p>");				
    					break;
    				
    					case 'url':
    					$validate = new validate;
    					if (!$validate->url($value)) $this->setData("error", "$key", "<p>$value är en felaktig URL</p>");
    				}
    			}
    			else $this->setData("error", "$key", "<p>$key måste fyllas i!</p>");
    		}
    	}
    	
    	/*
    	* ::Prepare
    	* Prepares actions for the page
    	*/
    	public function prepare() {
    		switch ($this->getData("site", "action")) {
    			case 'post':
    			if ($_POST)  {
    				$this->formCheck($_POST);
    				if (empty($this->registery["error"])) {
    					$headline = htmlspecialchars(str_replace("|", "", $_POST['headline']));
    					$name = htmlspecialchars(str_replace("|", "", $_POST['name']));
    					$email = $_POST['email'];
    					$text = htmlspecialchars(str_replace("|", "", $_POST['textarea']));
    					$time = date("Y:m:d");	
    					$instance = new guestbookModel;
    					$instance->write($headline, $name, $email, $text, $time);
    				}
    			}
    			break;
    			
    			case 'view':
    			$instance = new guestbookModel;
    			$this->setData("guestbook", "posts", $instance->read());
    			$guestbook = $this->getData("guestbook", "posts");
    			$lines = array_reverse(explode("\r\n", $guestbook));
    			foreach ($lines as $line) {
    				if (!empty($line)) $posts[] = explode("|", $line);
    			}
    			$this->setData("guestbook", "posts", $posts);
    			break;
    		}
    	}
    
    	/*
    	* ::Render
    	* Renders the page
    	*/
    	public function render() {
    		include $this->getData("site", "template");
    	}
    }
    ?>
    guestbookModell.php
    Code:
    <?php
    class guestbookModel {
    	private $file = 'guestbook.txt';
    	
    	/*
    	* ::Write
    	* Writes to the guestbook file
    	*/
    	public function write($headline, $name, $email, $text, $time) {
    		$content = file_get_contents($this->file);
    		$content .= "$headline|$name|$email|$text|$time" . "\r\n";
    		file_put_contents($this->file, $content);
    	}
    	
    	/*
    	* ::Read
    	* Reads from the guestbook file
    	*/
    	public function read() {
    		return file_get_contents($this->file);		
    	}
    }
    ?>
    view.tpl
    Code:
    <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    <?php
    foreach ($this->getData("guestbook", "posts") as $value) {
    ?>
    
    <p><strong>Rubrik</strong></p>
    <p><?php echo $value[0] ?></p>
    <p><strong>Av</strong></p>
    <p><?php echo $value[1] ?></p>
    <p><strong>E-Post</strong></p>
    <p><?php echo $value[2] ?></p>
    <p><strong>Meddelande</strong></p>
    <p><?php echo $value[3] ?></p>
    <p><strong>Skrivet</strong></p>
    <p><?php echo $value[4] ?></p>
    <hr />
    
    <?php
    }
    ?>
    <p><a href="index.php?module=guestbook">Visa gästboken</a></p>
    <p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a>
    post.tpl
    Code:
    <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    <?php 
    echo $this->printErrors();
    ?>
    <form method="post">
    <p><strong>Rubrik</strong></p>
    <p><input name="headline" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['headline'] ?>" size="30" /></p>
    <p><strong>Namn</strong></p>
    <p><input name="name" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['name'] ?>" size="30" /></p>
    <p><strong>E-Post</strong></p>
    <p><input name="email" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['email'] ?>" size="30" /></p>
    <p><strong>Text</strong></p>
    <p><textarea name="textarea" cols="50" rows="5"><?php if (isset($_POST['submit'])) echo $_POST['textarea'] ?></textarea></p>
    <p><input name="submit" type="submit" value="Skicka" /></p>
    </form>
    <hr />
    <p><a href="index.php?module=guestbook">Visa gästboken</a></p>
    <p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a></p>
    You might consider implementing the Front Controller pattern. That index.php file pretty much does that already. It would save you from having to write a new front controller ever time you start a project. Just a thought.

    PHPycho's idea of making a class for you're view isn't a bad one. It makes more sense to have the view handle the rendering of the output. A simple

    PHP Code:
    $controller->getView()-render(); 
    Would suffice.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have bookmarked two site regarding MVC Framework tutorials.
    Hope some of you may find this useful if you are starting to build your own framework.

    http://anantgarg.com/2009/03/13/writ...mework-part-1/
    http://www.phpro.org/tutorials/Model...oller-MVC.html

  6. #6
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx for all replys so far!

    Befor having viewed the links supplied about building MVC i have done some different coding.

    Any input on this?

    Structure
    PHP Code:
    Index.php

    .Includes
    ..Config.php
    ..Controller.php
    ..Model.php
    ..FrontController.php

    .Controllers
    ..Default
    ...
    DefaultController.php
    .Guestbook
    ..GuestbookController.php

    .Views
    ..Default
    ...
    Index.php
    ..Guestbook
    ...Index.php
    ...Post.php 
    The files

    root Index.php
    PHP Code:
    <?php
    include ('includes/Config.php');
    /**
    *    Handles URL input
    */
    $init FrontController::getInstance();
    $init->route();

    /**
    *    Sets correct classes and view
    */
    $controllerClass $init->getData("Site""ControllerClass");
    $modelClass $init->getData("Site""ModelClass");
    $view $init->getData("Site""View");
    $action $init->getData("Site""Action");

    $instance = new $controllerClass();
    $instance->setData("Site""View"$view);
    $instance->setData("Site""Action"$action);
    $instance->handle();
    $instance->render();
    ?>
    Controller.php
    PHP Code:
    <?php
    class Controller {
        private 
    $variables = array();
        
        
    /**
        *    Constructing the object and sets the view
        *    $instance = new Controller($view);
        */
        
    public function __construct() {
                
        }
        
        
    /**
        *    Set a variable
        *    $this->setData("field", "key", "value");
        */
        
    public function setData($field$key$value) {
            
    $this->variables[$field][$key] = $value;    
        }
        
        
    /**
        *    Get a variable
        *    $this->getData("field", "key");
        */
        
    public function getData($field$key) {
            return 
    $this->variables[$field][$key];
        }
        
        
    /**
        *    Renders page
        *    $instance->render();
        */
        
    public function render() {
            include (
    $this->getData("Site""View"));    
        }
    }
    ?>
    FrontController.php
    PHP Code:
    <?php
    /**
    *    FrontController Class.
    *
    */

    class FrontController {
        private 
    $variables = array();
        
        public function 
    __construct() {
        }
        
        
    /**
        *    Set a variable
        *    $this->setData("field", "key", "value");
        */
        
    public function setData($field$key$value) {
            
    $this->variables[$field][$key] = $value;    
        }
        
        
    /**
        *    Get a variable
        *    $this->getData("field", "key");
        */
        
    public function getData($field$key) {
            return 
    $this->variables[$field][$key];
        }
        
        
    /**
        *    Singleton function
        *    $variable = FrrontController::getInstance();
        */
        
    public function getInstance() {
            static 
    $instance;
            
            if (!isset(
    $instance)) {
                
    $class 'FrontController';
                
    $instance = new $class;
            }
            return 
    $instance;
        }
        
        
    /**
        *    Routes requested URL
        */
        
    public function route() {
            
    $page ucfirst(strtolower(empty($_GET['page']) ? 'default' $_GET['page']));
            
    $action ucfirst(strtolower(empty($_GET['action']) ? 'index' $_GET['action']));

            
    $model 'Models/' $page '/' $page 'Model.php';
            
    $controller 'Controllers/' $page '/' $page 'Controller.php';
            
    $view 'Views/' $page '/' $action '.php';
            
            if (!
    file_exists($model)) header("Location: index.php");
            if (!
    file_exists($controller)) header("Location: index.php");
            if (!
    file_exists($view))header ("Location: index.php");
            
            
    $this->setData("Site""Model"$model);
            
    $this->setData("Site""Controller"$controller);
            
    $this->setData("Site""Action"$action);
            
    $this->setData("Site""View"$view);

            
    $modelClass $page 'Model';
            
    $controllerClass $page 'Controller';

            
    $this->setData("Site""ModelClass"$modelClass);
            
    $this->setData("Site""ControllerClass"$controllerClass);
            
            include(
    $this->getData("Site""Model"));
            include(
    $this->getData("Site""Controller"));    
        }    
    }
    ?>
    GuestbookController.php
    PHP Code:
    <?php
    class GuestbookController extends Controller {
        public function 
    handle(){
            switch(
    $this->getData("Site""Action")) {
                case 
    'Index':
                
    /*
                Some viewing code here
                */
                
    break;
                
                case 
    'Post':
                
    /*
                Some posting code here
                */
                
    break;
            }
        }
    }
    ?>
    Models are so far clean with only class names in them.
    Default controller does nothing atm.

  8. #8
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by t3ch View Post
    Thanx for all replys so far!

    Befor having viewed the links supplied about building MVC i have done some different coding.

    Any input on this?

    Structure
    PHP Code:
    Index.php

    .Includes
    ..Config.php
    ..Controller.php
    ..Model.php
    ..FrontController.php

    .Controllers
    ..Default
    ...
    DefaultController.php
    .Guestbook
    ..GuestbookController.php

    .Views
    ..Default
    ...
    Index.php
    ..Guestbook
    ...Index.php
    ...Post.php 
    The files

    root Index.php
    PHP Code:
    <?php
    include ('includes/Config.php');
    /**
    *    Handles URL input
    */
    $init FrontController::getInstance();
    $init->route();

    /**
    *    Sets correct classes and view
    */
    $controllerClass $init->getData("Site""ControllerClass");
    $modelClass $init->getData("Site""ModelClass");
    $view $init->getData("Site""View");
    $action $init->getData("Site""Action");

    $instance = new $controllerClass();
    $instance->setData("Site""View"$view);
    $instance->setData("Site""Action"$action);
    $instance->handle();
    $instance->render();
    ?>
    Controller.php
    PHP Code:
    <?php
    class Controller {
        private 
    $variables = array();
        
        
    /**
        *    Constructing the object and sets the view
        *    $instance = new Controller($view);
        */
        
    public function __construct() {
                
        }
        
        
    /**
        *    Set a variable
        *    $this->setData("field", "key", "value");
        */
        
    public function setData($field$key$value) {
            
    $this->variables[$field][$key] = $value;    
        }
        
        
    /**
        *    Get a variable
        *    $this->getData("field", "key");
        */
        
    public function getData($field$key) {
            return 
    $this->variables[$field][$key];
        }
        
        
    /**
        *    Renders page
        *    $instance->render();
        */
        
    public function render() {
            include (
    $this->getData("Site""View"));    
        }
    }
    ?>
    FrontController.php
    PHP Code:
    <?php
    /**
    *    FrontController Class.
    *
    */

    class FrontController {
        private 
    $variables = array();
        
        public function 
    __construct() {
        }
        
        
    /**
        *    Set a variable
        *    $this->setData("field", "key", "value");
        */
        
    public function setData($field$key$value) {
            
    $this->variables[$field][$key] = $value;    
        }
        
        
    /**
        *    Get a variable
        *    $this->getData("field", "key");
        */
        
    public function getData($field$key) {
            return 
    $this->variables[$field][$key];
        }
        
        
    /**
        *    Singleton function
        *    $variable = FrrontController::getInstance();
        */
        
    public function getInstance() {
            static 
    $instance;
            
            if (!isset(
    $instance)) {
                
    $class 'FrontController';
                
    $instance = new $class;
            }
            return 
    $instance;
        }
        
        
    /**
        *    Routes requested URL
        */
        
    public function route() {
            
    $page ucfirst(strtolower(empty($_GET['page']) ? 'default' $_GET['page']));
            
    $action ucfirst(strtolower(empty($_GET['action']) ? 'index' $_GET['action']));

            
    $model 'Models/' $page '/' $page 'Model.php';
            
    $controller 'Controllers/' $page '/' $page 'Controller.php';
            
    $view 'Views/' $page '/' $action '.php';
            
            if (!
    file_exists($model)) header("Location: index.php");
            if (!
    file_exists($controller)) header("Location: index.php");
            if (!
    file_exists($view))header ("Location: index.php");
            
            
    $this->setData("Site""Model"$model);
            
    $this->setData("Site""Controller"$controller);
            
    $this->setData("Site""Action"$action);
            
    $this->setData("Site""View"$view);

            
    $modelClass $page 'Model';
            
    $controllerClass $page 'Controller';

            
    $this->setData("Site""ModelClass"$modelClass);
            
    $this->setData("Site""ControllerClass"$controllerClass);
            
            include(
    $this->getData("Site""Model"));
            include(
    $this->getData("Site""Controller"));    
        }    
    }
    ?>
    GuestbookController.php
    PHP Code:
    <?php
    class GuestbookController extends Controller {
        public function 
    handle(){
            switch(
    $this->getData("Site""Action")) {
                case 
    'Index':
                
    /*
                Some viewing code here
                */
                
    break;
                
                case 
    'Post':
                
    /*
                Some posting code here
                */
                
    break;
            }
        }
    }
    ?>
    Models are so far clean with only class names in them.
    Default controller does nothing atm.
    Guest books seems to be an implementation of the page controller pattern which is a bit superfluous if you already have a front controller. Your front controller can initialize the controller class and execute a specific function call. So instead of having a switch statement your controller would consist of several functions.

    PHP Code:
    class GuestbookController extends Controller {
      public function 
    execIndex(){/* */}
      public function 
    execPost(){/* */}

    Suffixing or prefixing the function is a good idea if you choose to use this method.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  9. #9
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply.

    But im still a bit confused. Your saying i should in my controller class decide what function that is run in my "extended" classes? That means i have to do a switch or a lot of IF´s in my controller, say a "prepare" function?

    example in Controller.php.

    PHP Code:
    public function prepare() {
    switch (
    $action) {
    case 
    'view':
    $extendexcontroller->view();
    break;

    case 
    'edit':
    $extendedclass->edit();
    break;
    }


  10. #10
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by t3ch View Post
    Thanks for reply.

    But im still a bit confused. Your saying i should in my controller class decide what function that is run in my "extended" classes? That means i have to do a switch or a lot of IF´s in my controller, say a "prepare" function?

    example in Controller.php.

    PHP Code:
    public function prepare() {
    switch (
    $action) {
    case 
    'view':
    $extendexcontroller->view();
    break;

    case 
    'edit':
    $extendedclass->edit();
    break;
    }

    Your front controller would be calling a function of your controller class. It would determine whether a given action exists and call the action of that controller class. PHP has built in functionality that allows you to check to see if a method exists in a class. You can use that to determine if a given action exists. The switch completely unnecessary.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  11. #11
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    t3ch, you seem to be concerned about your file structure. The file structure you choose normally comes down to personal preference. I like to divide my applications into "modules", each of which contains it's own "model" and "view" directories. One module, may have many actions, for example, a the "thread" module of a forum may have the following actions: show, create, delete, edit. Each action in my case, corresponds to a view. An action though can have multiple views for different types of output, for example, the "show" action for the "thread" module of a forum, may have a separate view for HTML, PDF and XML.

    Now, the advantage I find in separating out each module into it's own folder, is that it allows you to simply drag-and-drop a new module into your application. With how I design my applications, because of how I've designed the front controller, I wouldn't even have to edit any files for the new module to be usable.

    So here's an example of the structure I've been talking about...

    Code:
    Application
    ├ Common
    ├ Modules
    │ ├ Main
    │ ├ Topics
    │ ├ Topic
    │ │ ├ Actions
    │ │ │ ├ create.php
    │ │ │ ├ delete.php
    │ │ │ ├ edit.php
    │ │ │ └ show.php
    │ │ ├ Views
    │ │ │ ├ create.html.php
    │ │ │ ├ delete.html.php
    │ │ │ ├ edit.html.php
    │ │ │ ├ show.html.php
    │ │ │ └ show.pdf.php
    │ │ └ Controller.php
    │ ├ Login
    │ └ Search
    ├ Themes
    └ index.php
    Library

  12. #12
    SitePoint Enthusiast ivanfx's Avatar
    Join Date
    May 2007
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For God's sake, use an existing framework.
    Freelance CSS and PHP developer. Using CakePHP and jQuery.

  13. #13
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wardrop View Post
    t3ch, you seem to be concerned about your file structure. The file structure you choose normally comes down to personal preference. I like to divide my applications into "modules", each of which contains it's own "model" and "view" directories. One module, may have many actions, for example, a the "thread" module of a forum may have the following actions: show, create, delete, edit. Each action in my case, corresponds to a view. An action though can have multiple views for different types of output, for example, the "show" action for the "thread" module of a forum, may have a separate view for HTML, PDF and XML.

    Now, the advantage I find in separating out each module into it's own folder, is that it allows you to simply drag-and-drop a new module into your application. With how I design my applications, because of how I've designed the front controller, I wouldn't even have to edit any files for the new module to be usable.

    So here's an example of the structure I've been talking about...

    Code:
    Application
    ├ Common
    ├ Modules
    │ ├ Main
    │ ├ Topics
    │ ├ Topic
    │ │ ├ Actions
    │ │ │ ├ create.php
    │ │ │ ├ delete.php
    │ │ │ ├ edit.php
    │ │ │ └ show.php
    │ │ ├ Views
    │ │ │ ├ create.html.php
    │ │ │ ├ delete.html.php
    │ │ │ ├ edit.html.php
    │ │ │ ├ show.html.php
    │ │ │ └ show.pdf.php
    │ │ └ Controller.php
    │ ├ Login
    │ └ Search
    ├ Themes
    └ index.php
    Library
    Where does the model part goes (in Topic Module)?


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
  •