Hi anakadote
Thanks for the reply!
php code is printing out fine when finding posts $routes array
http://localhost:8080/localFixers/posts
but not when finding sessions $routes
I am defining $params in index.php
<?php
// starts a session
session_start();
/**
* dispatches route
* @param array $routes
* @return array
*/
include('config.php');
function dispatcher($routes)
{
// Requested URL
$url = $_SERVER['REQUEST_URI'];
// Removes Apllication root from url
$url = str_replace('/'.APP_ROOT.'/', '', $url);
// holds the named captures, $_POST data
$params = parse_params();
// Removes query string from $url we don't need it anymore affect routes.
$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $url);
// becomes true if $route['url'] matches $url
$route_match = false;
// loops over $routes looking for a match
foreach($routes as $urls => $route)
{
// if match found appends $matches to $params
// sets $route_match to true and also exits loop.
if(preg_match($route['url'], $url, $matches))
{
$params = array_merge($params, $matches);
$route_match = true;
break;
}
}
// if no route matched display error
if(!$route_match) { exit('no route found'); }
// include controller
include(CONTROLLER_PATH.$route['controller'].'.php');
if(file_exists(VIEW_PATH.'layouts'.DS.$route['controller'].'.php'))
{
// includes controller layout
include(VIEW_PATH.'layouts'.DS.$route['controller'].'.php');
}
else
{
// include default layout
include(VIEW_PATH.'layouts'.DS.'application.php');
}
// reset our flashs back to empty.
$_SESSION['flash']['notice'] = '';
$_SESSION['flash']['warning'] = '';
}
dispatcher($routes);
/**
* Return array of $_GET and $_POST data
* @return array
*/
function parse_params()
{
$params = array();
if (!empty($_POST))
{
$params = array_merge($params, $_POST);
}
if (!empty($_GET))
{
$params = array_merge($params, $_GET);
}
return $params;
}
?>
I am defining $routes in config.php
<?php
/**
* Holds the configuration for app, CONSTANTS ect...
* Good idea to look inside.
* change the directory of your app in here
* edit your routes in here.
*/
// routes url to controller and view
$routes = array(
// posts
array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
array('url' => '/^posts\/new$/', 'controller' => 'posts', 'view' => 'new'),
array('url' => '/^posts\/create$/', 'controller' => 'posts', 'view' => 'create'),
array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
array('url' => '/^posts\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
array('url' => '/^posts\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete'),
// homepage
array('url' => '/^(posts)?\/?(page\/)?(?P<page>\d?)$/', 'controller' => 'posts', 'view' => 'index'),
// sessions
array('url' => '/^sessions\/new$/', 'controller' => 'sessions', 'view' => 'new'),
array('url' => '/^sessions\/create$/', 'controller' => 'sessions', 'view' => 'create'),
array('url' => '/^sessions\/delete$/', 'controller' => 'sessions', 'view' => 'delete')
);
// The server root
define('SERVER_ROOT', $_SERVER['DOCUMENT_ROOT']);
//echo $_SERVER['DOCUMENT_ROOT'].'<br />';
// Directory structures
define('DS', '/');
// Application Directory - *** change this to your app folder. *** case sensative
define('APP_ROOT', 'localFixers');
//echo APP_ROOT;
//address of website *** change to http://localhost for virtual server ***
define('WEBSITE', 'http://localhost:8080/');
// MVC paths
define('CONTROLLER_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'controllers'.DS);
define('MODEL_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'models'.DS);
define('VIEW_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'views'.DS);
//echo CONTROLLER_PATH.'<br />';
//echo MODEL_PATH.'<br />';
//echo VIEW_PATH.'<br />';
//lib includes ...extends what files can do and shares with multiple applications
include ('lib/database.php');
include ('lib/controller.php');
include ('lib/model.php');
include ('lib/view.php');
?>