Results 1 to 10 of 10

Thread: Minimal MVC implementation

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Minimal MVC implementation

    Guys, I need help with a sample minimal MVC implementation. Just a simple one. Do any of you have links to some tutorials about this one? Or a good detailed article about creating a web framework?

    I planned on making my own web framework. Just a simple one. Something like, central controller gets the request, get the specific controller to some factory and execute the controller. Also, the controller knows the mapped Form, the form holds the data, return the View and the controller executes knowing all the details of what's the needed Model and View, displays the page then.

    Hmmm, sounds disgusting as I'm not really expert for this one. I'm just starting on, so any help is greatly appreciated. And, thanks in advance.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Minimal MVC implementation

    Database :: Platform Independent API (e.g PDO) --> Controller (formats output ) --> Template

    Input from form --> Factory --> Internal Redirect --> Processing -->Platform Independent API :: Database

    Keeping the input processing and output processing is the important and maintaining as much abstraction as possible. You want each part of your application to be de-coupled so if you decide you can replace for example the database type, or the output rendering format e.g: HTML,Text, CSV, RSS, Data Provider.

    You separate PHP files for separate tasks. It keeps things more logical and all your factory need do is analyse the script name and execute an appropriate function or controller based on that.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    Thanks vistalSad. Will do some search of the things you said. Again, thanks.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    I have some very very basic thing on this. I have the index.php which I consider to be the central controller. It looks like this (for now, improvements later).
    Code:
    $pathInfo = split('/', $_SERVER['PATH_INFO']);
    	
    	$controller = ucfirst($pathInfo[1]) . 'Controller';
    	$con = new $controller;
    	// call_user_func(array($controller, 'execute' . ucfirst($pathInfo[2])));
    	$con->executeList();
    	$con->notifyView($_SERVER['PATH_INFO'], $con->model);
    I have a problem to loosely coupled the one colored red. If I call call_user_func(array($controller, 'execute' . ucfirst($pathInfo[2])));, it's executing the method as static but then I need the instantiated object because of my controller which looks like
    Code:
    class PostController extends Controller {
    		var $model;
    		
    		function executeShow() {
    			$this->model = PostDao::readById($_REQUEST['id']);
    		}
    		
    		function executeList() {
    			$this->model = PostDao::findAll();
    		}
    	}
    and the base controller
    Code:
    <?php
    	class Controller {
    		
    		function intialize() {
    		}
    		
    		function execute() {
    		}
    		
    		function notifyView($page, $model) {
    			include(PAGE_DIR . $page . '.php');
    		}
    	}
    ?>
    You see, I assigned the $model from the DAO (which I don't know if this is a good design). I can then pass it to the notifyView passing the page to be included and the $model object.

    Right now I'm having the $con->executeList() as an explicit call but I need not to know the method to call, it should be the controller doing it. Any idea?

    Thanks in advance.

    Edit: Btw, an example of the path to call the PostController::executeList would be some.com/index.php/post/list

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Minimal MVC implementation

    Is this PHP4 or PHP5?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    It's 4

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Minimal MVC implementation

    First; don't use PHP4 /slap

    Second; it is not calling them method as static. When you assign variables in PHP 4 it always creates a copy of the variable. This is not desirable when using an object because you will then have two copies of the object. Use the reference operator when you make your call_user_func call and create new instances:
    PHP Code:
        $con = &new $controller;
        
    call_user_func(array(&$controller'execute' ucfirst($pathInfo[2])));
        
    $con->executeList() 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    Thanks. For now it's 4 (really I'm not comfortable with this but having difficulty convincing upgrade of apps). Will do some updates with this crappy MVC implementation and ask more.

    Again, thanks.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    notifyView is a mess. It has the $model variable which then include the page needed and on the page it has to be foreach ($model as $something) or like <?php echo $model->some; ?>. I need a named version of this thing. It would be good if I have in the controller like

    function executeList() {
    $this->posts = PostDao::findAll();
    }

    and on the page/post/list.php, foreach ($posts as $post) ... Is there anyway I can extract the PostController member variables as an array so that I can have the $posts variable in the included page/list.php?

    Anyone? Thanks so much.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Minimal MVC implementation

    Aha! I can do extract(get_object_vars($this)) so I can refer to $posts as a variable to be passed on the page that comes from the PostController's $this->posts = FromDao().


Posting Permissions

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



Click Here to Expand Forum to Full Width