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 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错