【问题标题】:CodeIgniter routes issues to access the frontend and backend folderCodeIgniter 路由问题以访问前端和后端文件夹
【发布时间】:2018-06-15 11:20:08
【问题描述】:

我正在使用 CodeIgniter。我在控制器和视图中有前端和后端文件夹。我尝试了服务器步骤,甚至检查了几乎所有的解决方案,但我仍然无法访问我的默认控制器

routes.php

$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

/*backend*********************************/
  $config['backend'] = 'backend/Access_control';  

1) 我的问题是当我访问 URL http://localhost/example_ci_row/

找不到 404 页面

2) 如何访问我尝试过的后端 URL http://localhost/icube_row/admin 但我得到了错误

前端用户控制

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

后端访问控制

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

class Access_control extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');           
    }

    public function index(){
        $this->load->view('backend/login');
    }

    }

?>

已编辑

当我使用以下步骤时它正在工作。我在控制器中添加了 Test.php 文件并更改了路由,然后我得到了登录页面。

Test.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

routes.php

$route['default_controller'] = 'Test';

【问题讨论】:

  • 可能是 .htaccess 文件丢失。请检查一次
  • @PravinVavadiya,htaccess 文件已在我的应用程序文件夹中可用。我应该在应用程序之外创建吗?
  • 我认为,路线可能有问题。
  • 内置的 $route['default_controller'] 不适用于子文件夹。您必须根据您的要求扩展系统路由器
  • @pradeep,你能帮我做更多吗?

标签: php codeigniter codeigniter-3


【解决方案1】:

希望对您有所帮助:

内置的$route['default_controller'] 不适用于子文件夹。您必须像这样根据您的要求扩展系统路由器:

你需要在application &gt; core &gt; MY_Router.php中创建一个MY_Router.php

<?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.');
    }
}

这将允许您使用$route['default_controller'] = 'frontend/User_control'; 作为您的默认控制器

【讨论】:

  • 是的,它正在工作。它是如何工作的?你能帮帮我吗?
  • 默认情况下我告诉你内置的 $route['default_controller'] 不适用于子文件夹。您必须根据您的要求扩展系统路由器
  • 我的默认控制器正在使用您的 MY_Router 代码,但如何访问后端控制器?
  • 是的,我肯定会接受它并为你的答案投票,但请给我一些时间在我的代码中实现。 default_controller 正在工作,但 $config['backend'] = 'backend/Access_control';不工作
  • 哦!上帝我添加了 $config instated 的路线。这就是它不起作用的原因。现在它工作得很好。感谢并从我身边投票。
【解决方案2】:

您是否配置了 Base URL应用程序->config->config.php

$config['base_url'] = 'http://localhost/example_ci_row';

【讨论】:

  • 我认为,路线可能有问题
  • 要解决其他问题,我们需要澄清默认控制器正在工作。当您在地址栏中尝试http://localhost/example_ci_row 时,根据这些设置它不会显示任何错误。
  • 是的,默认控制器正在工作,但是当我将控制器添加到前端和后端文件夹之外时它正在工作。
  • 我为您的问题找到了以前的答案。关注这个stackoverflow.com/questions/6529026/…
  • 在提问之前我已经尝试过了。如果我点击 URL localhost/example_ci_row/index.php/frontend/User_control 然后我得到我的登录页面但是当我点击 URL localhost/example_ci_row 然后它得到 404 page not found
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 2010-12-14
  • 1970-01-01
  • 2022-11-10
  • 2022-08-23
  • 2017-11-18
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多