How can I use MVC ideas without using classes?
As of right now, I am still shaky on classes, so I don't want to use any classes for my site. I'm still practicing with classes.
But how can I implement the MVC idea without classes?
Would this work for a MVC?
index.php (the view)
index_controller.php
index_model.php
Is this开发者_如何学Python right for what a MVC should be?
View: show html, css, forms
Controller: get $_POST from forms and any data from the user, get info from db
Model: do all the functions, insert/delete in db, etc
Basically separate the HTML/css for the view, all the data collecting for the controller, and the logic for the model. And just connect them all using require_once.
Controller: Your index.php
, accepting and directing requests. This can certainly be a 'classless' script. It would act as both controller and 'front controller'.
View(s): A collection of presentation scripts, specific script included by your controller. Essentially 'getting' data from the variable scope of the controller.
Model(s): A collection of functions that provide access to your data. The controller determines what to include for a request.
Sure, it can be done, but you loose a lot not using classes (OOP). Here's a quick example of what the controller might look like. Nothing amazing, just an idea. Showing the controller should shed some light on the model/view as well.
<?php
$action = getAction(); //parse the request to find the action
switch($action){
case 'list':
include('models/todolist.php'); //include the model
$items = todolistGetItems(); //get the items using included function
include('views/todolist/list.php'); //include the view
break;
case 'add':
if(!empty($_POST['new'])){
include('models/todolist.php'); //include the model
todolistAddItem($_POST); //add the item
$items = todolistGetItems(); //get the items using included function
include('views/todolist/list.php'); //include the view
} else {
include('views/todolist/add.php'); //include the view
}
}
精彩评论