0

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);
    }

}
2
  • 2
    Since you are setting up your own framework I suggest dropping all the statics everywhere. They only make it realy really hard to unit test your code. Instead look into dependency injection. Commented Jul 28, 2012 at 14:11
  • I'm removing the mvc tag. The question is completely unrelated to it. Commented Jul 28, 2012 at 14:58

2 Answers 2

3

If you plan to copy and paste a work, please understand the whole process first before assuming everything is plain static, do read up on:

Sign up to request clarification or add additional context in comments.

Comments

1

This is because you are only using local variables. When dealing with classes the objects variables are accessed via $this-> and static variables - with self:: or static::.

http://codepad.org/F7UWmOSb

class Foo
{
    protected static $bar = 'lorem';
    protected $buz = 'ipsum';

    public function test()
    {
        var_dump( $this->buz );
        var_dump( self::$bar );
    }

}


$instance = new Foo;
$instance->test();

Also, as @PeeHaa mentioned: stop using static variables. It is not OOP. Static structures are just global variables/functions wrapped in a namespace, which just happens to look like class definition.

And learn about dependency injection, these lectures might help :

2 Comments

The laravel framework is writen with static method i'm just copying there code. Because i dont to use the whole framework just to make my own simple/smaller version
Well i just want to have MVC architecture and I like the way in lavarel works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.