从 URL php 中检索参数
Retrieve parameter from URL php
我正在尝试实现这个 PHP MVC 应用程序。
我在这里的尝试是 add/edit/delete CATEGORIES。类别具有名称和描述。它的视图基本上是一个带有类别数据的 table 和一个 edit/delete 的 link。
我已经动态加载并完成了添加类别部分,但我卡在 edit/delete。
我的路由是这样设置的:Controller/Action/Parameter。
单击“编辑”link,应用程序获取行的 ID 并继续编辑页面,因此:/categories/edit/(categoryID)
所以如果我想编辑第一个条目,它会转到 categories/edit/1
我的问题是:如何检索类别 ID?那 1 来自 URL。
$_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 似乎不起作用。
你可以简单地使用这样的东西,
// 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);
我正在尝试实现这个 PHP MVC 应用程序。
我在这里的尝试是 add/edit/delete CATEGORIES。类别具有名称和描述。它的视图基本上是一个带有类别数据的 table 和一个 edit/delete 的 link。
我已经动态加载并完成了添加类别部分,但我卡在 edit/delete。
我的路由是这样设置的:Controller/Action/Parameter。
单击“编辑”link,应用程序获取行的 ID 并继续编辑页面,因此:/categories/edit/(categoryID)
所以如果我想编辑第一个条目,它会转到 categories/edit/1
我的问题是:如何检索类别 ID?那 1 来自 URL。
$_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 似乎不起作用。
你可以简单地使用这样的东西,
// 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);