dsxi70423 2013-05-15 13:08
浏览 29
已采纳

从uri获取参数

I setup a simple router class that extracts the following

  1. Controller
  2. Action
  3. Parameters

class Router {

private $uri;
private $controller;
private $method;
private $params;

public function __construct($uri) {
    $this->uri = $uri;
    $this->method = 'index';
    $this->params = array();
}

public function map() {
    $uri = explode('/', $this->uri);
    if (empty($uri[0])) {
        $c = new Config('app');
        $this->controller = $c->default_controller;
    } else {
        if (!empty($uri[1]))
            $this->method = $uri[1];
        // how about the parameters??
    }
}

}

That simple $router->map() can give me the right controller, action and a single parameter from this uri http://domain.com/users/edit/2

That is quite okay, but what if I needed to store more parameters in the url like this : http://domain.com/controller/action/param/param2/param3

How do I push them to $parms if I don't know how many parameters will be passed.

  • 写回答

1条回答 默认 最新

  • douben8492 2013-05-15 13:12
    关注

    You know the 1st two values of array are controller and action, everything after will be a param.

    So you could use array_shift($uri) to get the first 2 and the remaining $uri will be your params.

    public function map() {
        $uri = explode('/', $this->uri);
    
        // shift element off beginning of array.
    
        $controller = array_shift($uri);
        $action = array_shift($uri);
    
        // your $uri variable will not only contain the params.
    
        if (empty($uri[0])) {
            $c = new Config('app');
            $this->controller = $c->default_controller;
        } else {
            if (!empty($uri[1]))
                $this->method = $uri[1];
            // how about the parameters??
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#不用正则表达式如何全字匹配
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计