【问题标题】:Can't access second controller in codeigniter无法访问 codeigniter 中的第二个控制器
【发布时间】:2014-11-10 12:07:23
【问题描述】:

我已经为adminfront-end 创建了两个单独的控制器,我可以访问前端控制器并可以访问它的功能,但是当我尝试访问管理控制器时,我无法使用.htacess文件来重写url。我的url看起来像http://localhost/bookstore/index

前端控制器:

<?php 
class Bookstore extends CI_Controller {

   function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','form','file','cookie','captcha'));
        $this->load->library(array('session','pagination','form_validation'));
        $this->load->model('bkmodel');
    }

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

管理员-控制器:

class Adminstore extends CI_Controller {

   function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','form','file','cookie','captcha'));
        $this->load->library(array('session','pagination','form_validation'));
        $this->load->model('admin_model');
    }

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

路由文件如下所示:

$route['default_controller'] = "bookstore";
$route['(:any)'] = "bookstore/$1";
$route['404_override'] = '';

我被困在这里,为什么第二个控制器不工作,请指出我的错误,这样当我想访问管理员时,我可以再处理一件事,我的网址是这样的http://localhost/bookstore/adminstore/index,它说找不到 404 页面

【问题讨论】:

    标签: codeigniter codeigniter-2


    【解决方案1】:

    在几乎所有重定向的“任何”规则之前,您需要另一个路由规则。所以,这样的事情应该可以工作:

    $route['default_controller'] = "bookstore";
    $route['adminstore/(:any)'] = "adminstore/$1";
    $route['(:any)'] = "bookstore/$1";
    $route['404_override'] = '';
    

    请记住,规则是按顺序检查的,从上到下,所以任何不太具体/全局的东西都应该放在顶部。 “任何”规则应该在最底部。

    【讨论】:

    • 我是从头顶写的,所以没有经过测试。您可能需要对其进行调整以适合您的管理控制器,但这是您应该遵循的概念。
    • 真棒你的点击和想法是@Shomz
    猜你喜欢
    • 2016-06-12
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多