从URL php中检索参数

时间:2017-07-10 12:49:19

标签: php content-management-system url-routing

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

我已经动态加载并完成了添加类别部分,但我坚持编辑/删除。

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

点击编辑链接,应用程序抓取行的ID并进入编辑页面,所以:/ categories / edit /(categoryID)
 因此,如果我想编辑第一个条目,它将转到categories / edit / 1

我的问题是:如何检索类别ID?来自URL的那个1。


$ _ 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];
} 

道歉。这是route.php。 此外,使用array_pop来获取id似乎不起作用。

1 个答案:

答案 0 :(得分:0)

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

    // 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);
相关问题