1

Hello I'm trying to implement a url router in php something familiar to express.js Here is the code I have so far.

class Router{

    private $request;
    private $request_params;
    private $params_num;
    private $paths;

    public function __construct(){

        $this->paths = array();
        $this->request = strtolower($this->hookRequest());

        if ($this->request != false){
            $this->request_params = $this->hookRequestParams();
        } else {
            $this->request_params = array('home');
        }

    }

    public function __destruct(){
        foreach($this->paths as $key => $value){
            if($this->getRequest() == $key){
                $value();
            }
        }
    }

    public function get($path, $func){
        $this->paths[$path] = $func;
    }

    private function hookRequest(){
        return isset($_GET['req']) ? rtrim($_GET['req'], '/') : false;
    }

    private function hookRequestParams(){
        $params = explode('/', $this->request);
        $this->params_num = count($params);
        return $params;
    }

    public function getRequest(){
        return $this->request;
    }

    public function getRequestParams(){
        return $this->request_params;
    }

    public function getPage(){
        return $this->request_params[0];
    }

    public function getAction(){
        if($this->params_num > 1){
            return $this->request_params[1];
        }
        return false;
    }

    public function getActionParams(){
        if($this->params_num > 2){
            return $this->request_params[2];
        }
        return false;
    }

}

This is used like this as you can imagine:

$router = new Router();

$router->get('index', function(){
    echo 'index'; //index is being shown to the browser as expectd
    echo $this->getPage(); // This does not work apparently 
})

My issue is how could I execute $router methods from within the anonymous function? As shown in this example with $this->getPAge();

2
  • Guess you have to initialize the object there with new Router() ... or you use function ... use($var[here $obj]) Commented Sep 21, 2013 at 20:36
  • In PHP 5.4+ you can bind the closure. It's a bit messy, but it works. php.net/manual/en/closure.bind.php Commented Sep 21, 2013 at 20:54

1 Answer 1

4

Use a closure..

$router->get('index', function() use ($router) {
    echo 'index';
    echo $router->getPage();
})

If you define your Closure within your class, $this should be work.

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

2 Comments

Could you please provide an example or a reference I'm not so experienced with php, thank you.
http://www.php.net/manual/en/functions.anonymous.php "Closures may also inherit variables from the parent scope"

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.