Reputation: 10581
I've been writing procedurally for several years now and recently I decided to make the leap to Object Orientated code.
To help me get on the right footing I've been working on an MVC framework of my own. I'm aware of Zend and so forth, but I just want something elegant and lightweight where I understand everything 100% and can build up knowledge. However, I need a little help and advice.
Basic folder architecture is:
/view/
/controller/
/model/
index
.htaccess
These are the files I have so far:
/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
/index.php
//autoload new classes
function __autoload($class)
{
$folder = explode('_', $class);
require_once strtolower(str_replace('_', '/', $class)).'_'.$folder[0].'.php';
}
//instantiate controller
if (!isset($_GET['controller'])) { $_GET['controller'] = 'landing'; }
$controller_name = 'controller_'.$_GET['controller'];
new $controller_name($_GET,$_POST);
/controller/base_controller.php
abstract class controller_base
{
//store headers
protected $get;
protected $post;
//store layers
protected $view;
protected $model;
protected function __construct($get,$post)
{
//store the header arrays
$this->get = $get;
$this->post = $post;
//preset the view layer as an array
$this->view = array();
}
public function __destruct()
{
//extract variables from the view layer
extract($this->view);
//render the view to the user
require_once('view/'.$this->get['controller'].'_view.php');
}
}
/controller/landing_controller.php
class controller_landing extends controller_base
{
public function __construct($get,$post)
{
parent::__construct($get,$post);
//simple test of passing some variables to the view layer
$this->view['text1'] = 'some different ';
$this->view['text2'] = 'bit of text';
}
}
Question 1) Is this framework laid out correctly?
Question 2) How should I integrate the model layer into this framework?
Question 3) Any other suggestions about how to improve this?
Upvotes: 1
Views: 1740
Reputation: 1016
Well, I'll try to answer the best, I can.
Well, that's subjective. If it's how you want to work, then yes it is! What I do different is, that in my htaccess I just pass EVERYTING that's after "http://domain.com/" as a parameter to a get-parameter and then processes the data in my PHP. E.g. like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?urlparam=$1 [NC,L,QSA]
And then processes it in PHP:
$_GET['urlparam'];
# Do whatever here. Maybe explode by "/".
This last thing is the routing part. I just make patterns that i match to the URL.
E.g. /advertisements/:id
leads to \Advertisements\Show
I am running a load-class, which I call, when I need to load either a view, a model, a plugin or any other file into my controller. In that way I am sure, that the file is only loaded once. The load-model function just returns an object with the model, in that way I have it instantiated and can use it later on.
You should probably read some tutorials. I think, that Anant Garg is explaining it very well in this tutorial: http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/
But there is a lot of them "out there":
E.g. this one: http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
Or this one that gives 12 different approaches: http://www.ma-no.org/en/content/index_12-tutorials-for-creating-php5-mvc-framework_1267.php
Hope this helps you a little in the right direction,
Have a nice evening. :)
Upvotes: 1