【问题标题】:Routing system with array - Sending parameter with variable带数组的路由系统 - 使用变量发送参数
【发布时间】:2017-11-05 14:59:37
【问题描述】:

我尝试用 PHP 构建一个路由系统。我对可更改的 URL 有疑问。

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;
}

问题在于包含abc/my_custom_variable_or/slug的网址。

我需要获取my_custom_variable_orslug 并将它们放入params['fdsaf'] 以便我可以在我的控制器中使用它

我找到了 else if 的临时解决方案,例如:else if (preg_match("/blog\/*/", $this->query)) .... explode() 等...

为了让我的系统更灵活,我需要在$routes 数组中创建一些东西。路由数组将是一个不同的文件。

===示例输入和预期结果===

网址:博客

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

网址:blog/my-first-post

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

网址:blog/user/martin/page2(page2 是可选的)

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

【问题讨论】:

  • 我编辑问题并添加示例输入和预期结果

标签: php arrays regex model-view-controller routing


【解决方案1】:

我的正则表达式模式将捕获 3 个组。第一个是前导文本——对于所有三个样本输入,这是blog。第二组要么是空的,要么是user——这标识了何时使用GetUserPost。第三组可能为空或包含slug 值。

Pattern Demo

代码(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]];
    }
}

硬编码Blog 版本:

$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);
}

输出(来自任一方法):

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',
  ),
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多