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!
The easiest way to handle this is to use the call_user_func_array() function. You would use it as follows:
$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.