Php 如果控制器或方法不存在,则将url重定向到404页
如果URL包含不存在的控制器和方法,我想将用户重定向到404页面Php 如果控制器或方法不存在,则将url重定向到404页,php,url-routing,Php,Url Routing,如果URL包含不存在的控制器和方法,我想将用户重定向到404页面 <?php define('WEBROOT',str_replace('index.php','',$_SERVER['SCRIPT_NAME'])); //echo (WEBROOT .'<br/>'); define('ROOT',str_replace('index.php','',$_SERVER['SCRIPT_FILENAME'])); //echo (ROOT .'<br/>');
<?php
define('WEBROOT',str_replace('index.php','',$_SERVER['SCRIPT_NAME']));
//echo (WEBROOT .'<br/>');
define('ROOT',str_replace('index.php','',$_SERVER['SCRIPT_FILENAME']));
//echo (ROOT .'<br/>');
require_once(ROOT."core/Controller.php");
$url = $_GET["p"];
$urlArray = array();
$urlArray = explode("/", $url);
$controller = !empty($urlArray[0]) ? $urlArray[0] : "Home";
$methodOrAction = !empty($urlArray[1]) ? $urlArray[1] : "index";
echo $controller ."<br/>";
echo $methodOrAction ."<br/>";
if (file_exists("controllers/".$controller.".php") && method_exists($controller,$methodOrAction)){
require_once("controllers/".$controller.".php");
$controller = new $controller();
$controller->$methodOrAction();
}else{
// echo '404 : This file or method is not exist !';
require_once("controllers/ErrorMsg.php");
$ErrorMsg = new ErrorMsg();
$ErrorMsg->DisplayError();
}
将某人重定向到默认404页面对用户不友好。相反,您可以通过以下方式创建错误页面并将用户重定向到该页面:
Header("Location: /404.html");
这会将访问者重定向到www.yourwebsite.com/404.html。您可以按任何方式自定义链接。谢谢,但实际上我不想使用标题功能,我想使用重定向作为控制器,正如您在codeWell中看到的那样-然后您需要更具体地说明这一点。你能提供更多关于系统整体结构的细节吗?我看不出这里有错误。你能更明确地说明你得到了什么和想要得到什么吗?嗨,if()语句不起作用,因为当我在URL中输入任何不存在的控制器或操作时,结果是不可接受的