MVC set up
Posted: Sun Jul 01, 2007 9:41 am
Playing a bit with an MVC project and would like to know if what I have so far is in the right direction. I'll not show all code, as that would be a bit too much, but the 3 tiers from the mvc part. I have a frontcontroller who dispatches to the different controllers.
In case of the URL /routes/view/ it maps to the controller "routes" and the action "view". I know the code is quite messy, but as a start I would like to know if the general direction is ok. I have the controller with the main actions. The controller sets the view. The view instantiates the model and gets the data from the model, put it in the template.
So the controller:
Then the view
Then the model
Last the template routes_view.php
It's a bit of php4 and 5 code mixed together, and another thing to do is get the naming conventions right. My main concerns are if the responsibilities are ok. And some methods will probably grow a lot when I add more stuff to it, but I guess I can refactor that out by adding helper functions and /or helperview classes.
The view also must get access to extra parameters somehow (/view/2007/10/ for example) and I'll have to add authentication (so that you only view your own routes).
In case of the URL /routes/view/ it maps to the controller "routes" and the action "view". I know the code is quite messy, but as a start I would like to know if the general direction is ok. I have the controller with the main actions. The controller sets the view. The view instantiates the model and gets the data from the model, put it in the template.
So the controller:
Code: Select all
<?php
include $_SERVER['DOCUMENT_ROOT'] . ('/app/views/routes_view.php');
class routes {
var $response;
function routes(&$locator){
$this->response =& $locator->get('Response');
}
function run(&$locator) {
$content = 'bla bla ';
$this->response->setContent($content);
}
function view(&$locator){
$view = new routesview();
$content = $view->viewroutes(3); // 3 is hardcoded for now
$this->response->setContent($content);
}
function insert($locator){}
function edit(&$locator){}
}
?>Code: Select all
<?php
require_once('/app/models/routeclimb.php');
require_once('A/Template.php');
class routesview {
public $template;
public function __construct()
{
$this->model = new RouteClimbModel();
$this->template = new A_TemplateInclude('tpl/routes_view.php');
}
public function viewroutes($id)
{
// get the data from the model
$routes = $this->model->listRouteClimbs($id);
$html = '';
if($routes){
// construct the HTML for the main content from the data I got from the model
$html = '';
foreach ($routes as $key => $value){
$html .= '<ul>';
$html .= '<li>Climb_id is: ' . $value['climb_id'] . '</li>'
. '<li>Climb_notes: '. $value['climb_notes'] . '</li>';
$html .= '</ul>';
}
} else {
$html = 'Currently no routes';
}
// render the template and content
$this->template->set('maintitle', 'This is the main title');
$this->template->set('subtitle', 'This is the subtitle');
$this->template->set('maincontent', $html);
echo $this->template->render();
}
public function insertroutes(){ }
}Code: Select all
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . ('/lib/DBPDO.php');
class RouteClimbModel {
public $climb_id;
public $route_id;
public $user_id;
public $climb_date;
public $climb_style;
public $climb_tries;
public $climb_value;
public $climb_notes;
public $climb_dateposted;
public function __construct(){
$this->conn = DBpdo::conn();
}
public function save(){ }
public function update(){ }
public function listRouteClimbs($userid){
$stmt = $this->conn->prepare( "SELECT * FROM mc_routeclimbs WHERE user_id = :user_id" );
$stmt->bindParam( ":user_id", $userid );
$rs = $stmt->execute();
if ($rs) {
$result = array();
while( $row = $stmt->fetch() ) {
$result[] = $row;
}
return $result;
}
else
{
trigger_error('DB error: ' . $this->conn->errorMsg());
}
}
}Code: Select all
<html>
<body>
<p><a href="/">Home</a> | <a href="/training/">Training</a> | <strong><a href="/routes/">Routes</a></strong> | <a href="/boulders/">boulders</a></p>
<p><strong><a href="/routes/view/">View</a></strong> | <a href="/routes/edit/">edit</a> | <a href="/routes/insert/">insert</a></p>
<p>You are here: Front controller: Routes! Action:view</p>
<h1><?php echo $this->data['maintitle'];?></h1>
<h2><?php echo $this->data['subtitle'];?></h2>
<div id="maincontent">
<?php echo $this->data['maincontent'];?>
</div>
</body>
</html>The view also must get access to extra parameters somehow (/view/2007/10/ for example) and I'll have to add authentication (so that you only view your own routes).