我有一个基本的PHP路由系统,当它与所有插入的网址都不匹配时,我想知道如何将其重定向到404错误?
这是Router.php
文件”
class Route
{
/**
* @var array $_listUri List of URI's to match against
*/
private $_listUri = array();
/**
* @var array $_listCall List of closures to call
*/
private $_listCall = array();
/**
* @var string $_trim Class-wide items to clean
*/
private $_trim = '/\^$';
/**
* add - Adds a URI and Function to the two lists
*
* @param string $uri A path such as about/system
* @param object $function An anonymous function
*/
public function add($uri, $function)
{
$uri = trim($uri, $this->_trim);
$this->_listUri[] = $uri;
$this->_listCall[] = $function;
}
/**
* submit - Looks for a match for the URI and runs the related function
*/
public function submit()
{
$uri = isset($_REQUEST['uri']) ? $_REQUEST['uri'] : '/';
$uri = trim($uri, $this->_trim);
$uri = strtolower($uri);
$replacementValues = array();
/**
* List through the stored URI's
*/
foreach ($this->_listUri as $listKey => $listUri)
{
/**
* See if there is a match
*/
if (preg_match("#^$listUri$#", $uri))
{
/**
* Replace the values
*/
$realUri = explode('/', $uri);
$fakeUri = explode('/', $listUri);
/**
* Gather the .+ values with the real values in the URI
*/
foreach ($fakeUri as $key => $value)
{
if ($value == '.+')
{
$replacementValues[] = $realUri[$key];
}
}
/**
* Pass an array for arguments
*/
call_user_func_array($this->_listCall[$listKey], $replacementValues);
}
}
}
}
`
然后将这个Router.php
包含在我的index
文件中,并调用此函数:
`
$route->add('/menu', function() {
include 'menu.php';
});
$route->submit();
`
我想知道我是否写
这样的网址
www.domain.com/nothing
代替www.domain.com/menu
它应该重定向到404错误页面
预先感谢