【问题标题】:404 page not found for codeigniter MVC while front_end setup前端设置时找不到 codeigniter MVC 的 404 页面
【发布时间】:2020-05-08 17:45:04
【问题描述】:

我已经搜索过这个但找不到任何有用的解决方案,所以我在这里发布我的问题。我正在为我的项目使用 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

routes.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”错误。有人请指出我正确的方向吗?谢谢。

【问题讨论】:

  • 尝试从默认控制器中删除索引,即$route['default_controller'] = 'front_end/front_end';
  • 路由['default_controller'] = 'front_end/front_end/index';不是一个类。应该是 route['default_controller'] = 'front_end/front_end'
  • 我已经尝试过没有索引为front_end/front_end,但效果很好。 @sauhardnc @fraggley
  • $route['default_controller'] = 'front_end'; 然后打开http://localhost/project_name/front_end 。如果您希望它仅在http://localhost/project_name 上打开,则将front_end 文件夹的内容直接移动到controller 文件夹中。
  • 你为什么没有回来看答案?

标签: php codeigniter codeigniter-3


【解决方案1】:

ci3就是这样设计的
你不能以嵌套的方式设置默认控制器..

但如果你仍然想这样做。你可以这样做

设置$route['default_controller'] = 'home';

home.php 应该在控制器的根路径/controller/home.php 并在此处包含您的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//requiring home controller
require_once APPPATH."controllers/front_end/Front_end.php";

【讨论】:

    【解决方案2】:

    这个问题的两个答案。

    1. 要使用 CodeIgniter 的路由默认设置,仅使用直接位于控制器文件夹 $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

    1. 通过用您自己的类覆盖 CodeIgniter 路由器类,为此在路径 application/core 中添加 my_router.php 文件,在该文件中复制并粘贴以下代码:

    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,然后你就会得到你想要的。

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 2020-07-14
      • 2019-12-02
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      相关资源
      最近更新 更多