|
-
Jul 10th, 2007, 07:42 PM
#1
Thread Starter
Fanatic Member
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.
-
Jul 20th, 2007, 06:37 PM
#2
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.
-
Jul 22nd, 2007, 07:38 PM
#3
Thread Starter
Fanatic Member
Re: Minimal MVC implementation
Thanks vistalSad. Will do some search of the things you said. Again, thanks.
-
Jul 26th, 2007, 08:26 PM
#4
Thread Starter
Fanatic Member
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
-
Jul 27th, 2007, 02:44 AM
#5
Re: Minimal MVC implementation
-
Jul 27th, 2007, 02:47 AM
#6
Thread Starter
Fanatic Member
Re: Minimal MVC implementation
-
Jul 27th, 2007, 04:38 AM
#7
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()
-
Jul 29th, 2007, 08:02 PM
#8
Thread Starter
Fanatic Member
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.
-
Jul 30th, 2007, 01:56 AM
#9
Thread Starter
Fanatic Member
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.
-
Jul 30th, 2007, 07:47 PM
#10
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|