3

I'm writing a custom MVC framework for a PHP project and everything is great until it comes to getting arguments from the URL route. I'm stuck on passing parts of the URL route into a function argument dynamically. I already have a method which is just to implode the route and use the array for the function arguments but I would really like to know how to do it like in CodeIgnitor or CakePHP.

Here's what i want to have done. The site url would be...

url: http://yoursite.com/profile/view/35/foo

and in my controller I would have...

<?php

Class profileController Extends baseController 
{

    public function view($ID, $blah)
    {
        echo $ID; //would show 35
        echo $blah; //would show foo
    }

}

?>

I would really like to know how this is done. Thanks a lot!

Daniel G
  • 63
  • 1
  • 5

3 Answers3

6

The easiest way to handle this is to use the call_user_func_array() function. You would use it as follows:

call_user_func_array(array($controller, $method), $params);

$controller would be the controller object you have already created, and $method would be the controller's method. Then $params is an array of the parameters collected from the URI. You would just need to take out the controller/method portion of the URI.

You could also do this using Reflection, but this typically is slower than using the above method.

Dan Horrigan
  • 1,275
  • 7
  • 8
  • Yes thank you very much, I was doing some research and the call_user_func_array is what I found and it worked perfectly. Once again thanks! – Daniel G Nov 06 '10 at 03:10
3

For anyone who wants to know what I ended up doing, the final code is below... (this is from my router.class.php)

<?php

$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
$this->route = explode('/', $route);

/*** a new controller class instance ***/
$class = $this->controller . 'Controller';
$controller = new $class($this->registry);

/*** load arguments for action ***/
$arguments = array();
foreach ($this->route as $key => $val) 
{
    if ($key == 0 || $key == 1)
    {
    }
    else
    {
        $arguments[$key] = $val;
    }
}

/*** execute controller action w/ parameters ***/
call_user_func_array(array($controller, $action), $arguments);

?>

if my URL was

http://mysite.com/documentation/article/3

my controller looks like this...

<?php

Class documentationController Extends baseController 
{

    public function article($article_ID = '')
    {
        echo $article_ID; //shows 3
    }

}

?>

Thanks for the help.

Daniel G
  • 63
  • 1
  • 5
  • I like the simplicity of your answer, however I'm not sure how to put it all together. For example I put your code into my index.php and I am generating an on baseController, that class is not found. Can you explain a bit more? – Iannazzi Sep 12 '15 at 08:08
1

Simply put, RewriteRules handle this. However within each framework is more complex routing code that directs requests and data to the specific Controller.

My suggestion would be to look at the code within these other frameworks and research how they solve these problems. Those you mentioned are open source.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • I have a mod_rewrite in a .htaccess file that does "mask" a $rt variable. I have access to the URL's route by $route[0], $route[1], etc.. I guess my real question is how to i turn that array of URL parts into function arguments in the format I have above. I have looked at both codeigniter and cakePHP as well as another framework but i just can't seem to find the code that creates these argument variables. – Daniel G Nov 06 '10 at 02:13