Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

model view controller - turn URL route into funciton arguments php mvc

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...