php - 前端设置时找不到 codeigniter MVC 的 404 页面
问题描述
我已经搜索过这个但找不到任何有用的解决方案,所以我在这里发布我的问题。我正在为我的项目使用 codeigniter MVC。我已经很好地设置了后端/管理面板,但是对于前端部分,当我尝试在 codeigniter MVC 中设置前端时出现“404 page not found”错误。
我的应用程序结构是:
project_name
--application
controllers
-- admin
-- front_end
-- Front_end.php
models
-- admin
-- front_end
-- Mdl_front_end.php
views
-- admin
-- front_end
-- home.php
路由.php
$route['default_controller'] = 'front_end/front_end/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;
$route['admin'] = 'admin/login';
$route['admin/gallery'] = 'admin/gallery/add';
$route['about'] = 'front_end/about';
核心/MY_Controller.php
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
}
?>
应用程序/控制器/front_end/Front_end.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Front_end extends MY_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('front_end/home');
}
}
每当我访问根 URL 时,例如:http://localhost/prject_name/得到“404 Page Not Found”错误。有人请指出我正确的方向吗?谢谢。
解决方案
这个问题的两个答案。
要使用 CodeIgniter 的路由默认设置,直接使用位于 controllers 文件夹中的控制器
$route['default_controller'] = 'controllerName'
,使用此选项无法使用子文件夹,例如controllers/subfolder/yourcontroller.php。没有办法做这样的事情$route['default_controller'] = 'subfolder/yourcontroller';
。幸运的是,我们可以通过为控制器文件夹中的每个子文件夹创建新路由来为该路径指定一个默认控制器来操作 CodeIgniter。让我们在你的项目中看到这个,你有controllers/front_end/front_end.php
,所以你必须去application/config/routes.php
添加以下行:$route['front_end'] = 'front_end/front_end';
然后打开浏览器写http://localhost/projectname/front_end,相当于http://localhost/projectname/front_end/front_end/index。
- 通过您自己的类覆盖 CodeIgniter 路由器类,为此在路径中添加 my_router.php 文件
application/core
,在该文件中复制并粘贴以下代码:
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
完成此操作后,转到您的 application/config/routes.php 并设置您的默认控制器:
$route['default_controller'] = 'front_end/fornt_end';
然后在你的浏览器中输入:http://localhost/projectname,然后你会得到你想要的。
推荐阅读
- android - 在初始屏幕期间初始化主要活动
- ruby-on-rails - 当我们在它之前放置一个 where 子句时,destroy_all 在事务中不起作用
- reactjs - reactjs中条件渲染的更简洁方式
- hibernate - 如何用like运算符编写jpql查询
- .net - 是否可以突出显示 winforms 列表视图的特定列标题?
- mule - 如何在 mulesoft 中检测文件中的拉丁词
- javascript - 引导表:如何动态提取 ajaxRequest(params) 调用所需的参数
- multithreading - 处理“线程中的异常..” tkinter,转换为“messagebox.showerror”
- java - 如何获取传递给变量的值?
- html - 带有 SCSS 的特殊悬停效果,没有代码冗余