doubi1910 2017-06-04 21:00
浏览 39

带数组的路由系统 - 带变量的发送参数

I try to build a routing system with PHP. I have a problem with changeable URLs.

private $routes = array(
    "blog" => array("Blog", "GetAll"),
    "/blog\/*/" => array("Blog", "GetOne"),
);

private $query;

public function __construct()
{
    $this->query = filter_var($_SERVER['QUERY_STRING'], FILTER_SANITIZE_URL);
}

public function SendAction()
{
    $route = array();
    if (array_key_exists($this->query, $this->routes)) {
        $route['controller'] = $this->routes[$this->query][0];
        $route['action'] = $this->routes[$this->query][1];
        $route['params'] = $this->routes[$this->query][2];
    } else {
        $route['controller'] = "Error";
        $route['action'] = "Main";
        $route['params'] = array();
    }
    return $route;
}

The problem is with the url that contains abc/my_custom_variable_or/slug.

I need to get my_custom_variable_or and slug and put them into params['fdsaf'] so I can use it in my controller

I find a temp solution with else if, like: else if (preg_match("/blog\/*/", $this->query)) .... explode() etc...

To make my system more flexible, I need to make something in $routes array. The routes array will be a different file.

===Sample inputs and the expected results===

URL: blog

$route['controller'] = Blog
$route['action'] = GetAll
$route['params'] = array()

URL: blog/my-first-post

$route['controller'] = Blog
$route['action'] = GetOne
$route['params'] = array('slug' => 'my-first-post')

URL: blog/user/martin/page2 (page2 is optional)

$route['controller'] = Blog
$route['action'] = GetUserPost
$route['params'] = array('slug' => 'martin')
  • 写回答

1条回答 默认 最新

  • dongzhong8691 2017-06-05 01:20
    关注

    My regex pattern will capture 3 groups. The first is the leading text -- for all three samples inputs, this is blog. The second group will either be empty or user -- this identifies when GetUserPost is to be used. The third group may be empty or contain the slug value.

    Pattern Demo

    Code (PHP Demo):

    if(preg_match('/^([^\/]+)\/?((?:user)?)\/?((?:[^\/]+)?)/',$v,$out)){
        $route['controller']=ucfirst($out[1]);
        if($out[2]==''){
            if($out[3]==''){
                $route['action']='GetAll';
                $route['params']=[];
            }else{
                $route['action']='GetOne';
                $route['params']=['slug'=>$out[3]];
            }
        }else{
            $route['action']='GetUserPost';
            $route['params']=['slug'=>$out[3]];
        }
    }
    

    Hard-coded Blog version:

    $inputs=['blog','blog/my-first-post','blog/user/martin/page2'];
    foreach($inputs as $v){
        $route=[];
        if(preg_match('/^[^\/]+\/?((?:user)?)\/?((?:[^\/]+)?)/',$v,$out)){
            $route['controller']='Blog';
            if($out[1]==''){
                if($out[2]==''){
                    $route['action']='GetAll';
                    $route['params']=[];
                }else{
                    $route['action']='GetOne';
                    $route['params']=['slug'=>$out[2]];
                }
            }else{
                $route['action']='GetUserPost';
                $route['params']=['slug'=>$out[2]];
            }
        }
        var_export($route);
    }
    

    Output (from either method):

    array (
      'controller' => 'Blog',
      'action' => 'GetAll',
      'params' => 
      array (
      ),
    )array (
      'controller' => 'Blog',
      'action' => 'GetOne',
      'params' => 
      array (
        'slug' => 'my-first-post',
      ),
    )array (
      'controller' => 'Blog',
      'action' => 'GetUserPost',
      'params' => 
      array (
        'slug' => 'martin',
      ),
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用