turn URL route into funciton arguments php mvc

后端 未结 3 1868
野的像风
野的像风 2020-12-19 23:13

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

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 23:53

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

    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...

    
    

    Thanks for the help.

提交回复
热议问题