Recently I started learning Laravel and is really good PHP framework. Currently I'm trying to make my own MVC similar how Laravel works.
My logic
We set the routes, then we check if the current URL exist and from there we will set the controller and the method from 'uses', if not we will set the controller to ERROR.
The problem is that I can't access the $routes variable.
If i try static::$routes = .. the php shows an error, but Lavarel is working ok even though my PHP version is 5.2 . I don't understand how the whole thing works. Also if you have any suggestions how to structure the architecture :)
index.php
include 'router.php';
include 'route.php';
Route::get('about',array('uses'=> 'about@index'));
Route::get('about2',array('uses'=> 'about@index'));
router.php
class Router {
public static $routes = array(
'GET' => array(),
'POST' => array(),
'PUT' => array(),
'DELETE' => array(),
'PATCH' => array(),
'HEAD' => array(),
);
public $valid = false;
public static $methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD');
public static function register($method, $route, $action)
{
if (ctype_digit($route)) $route = "({$route})";
if (is_string($route)) $route = explode(', ', $route);
foreach ((array) $route as $uri){
if (is_array($action)){
$routes[$method][$uri] = $action;
}
}
$request = $_SERVER['REQUEST_URI'];
$request = trim($request, '/');
$exist = array_key_exists($request, $routes[$method]);
if($exist){
$valid = true;
}
echo $valid;
// print_r($routes);
}
}
route.php
class Route {
public static function get($route, $action)
{
Router::register('GET', $route, $action);
}
}
statics everywhere. They only make it realy really hard to unit test your code. Instead look into dependency injection.mvctag. The question is completely unrelated to it.