从URL php检索参数

从URL php检索参数,php,content-management-system,url-routing,Php,Content Management System,Url Routing,我正在尝试实现这个PHP MVC应用程序。 我在这里尝试添加/编辑/删除类别。类别有名称和描述。它的视图基本上是一个带有类别数据和编辑/删除链接的表 我已经动态加载并完成了添加类别部分,但我仍停留在编辑/删除阶段 我的路由设置如下:Controller/Action/Parameter 单击编辑链接,应用程序获取行的ID并继续编辑页面,因此:/categories/EDIT/(categoryID) 因此,如果我想编辑第一个条目,它将转到categories/edit/1 我的问题是:如何检索类

我正在尝试实现这个PHP MVC应用程序。 我在这里尝试添加/编辑/删除类别。类别有名称和描述。它的视图基本上是一个带有类别数据和编辑/删除链接的表

我已经动态加载并完成了添加类别部分,但我仍停留在编辑/删除阶段

我的路由设置如下:Controller/Action/Parameter

单击编辑链接,应用程序获取行的ID并继续编辑页面,因此:/categories/EDIT/(categoryID)
因此,如果我想编辑第一个条目,它将转到categories/edit/1 我的问题是:如何检索类别ID?这是URL中的1


$\u GET['id']不起作用,因为我的路由看起来不像categories/edit.php?id=etc

编辑

<?php
$routes = explode('/', $_SERVER['REQUEST_URI']);

// get controller name
if (isset($routes[1]) && !empty($routes[1])) {
    $controllerName = $routes[1];
}

// get name action
if (isset($routes[2]) && !empty($routes[2])) {
    $actionName = $routes[2];
}

if (isset($routes[3]) && !empty($routes[3])) {
    $param = $routes[3];
} 

您可以简单地使用这样的东西

    // Get the current URL
    $url = $_SERVER['REQUEST_URI'];

    // Explode with /
    $parts = explode('/', rtrim($url, '/'));

    // Pop the element in the end of the array
    $id = array_pop($parts);

你得到了什么
print\r($\u REQUEST)
给我们看一些代码,你的链接看起来像一个REST API(也可能是关于这个的搜索)发布你的路由规则,这会让你清楚地知道1传递了什么。你在使用像FastRoute这样的路由库吗?我相信其中一个使用正则表达式。你的意思是这样的: