将索引控制器定义为codeigniter中的默认控制器

时间:2017-06-15 12:05:03

标签: php codeigniter-3

我将尝试将索引控制器定义为默认控制器

我还会更改routes.php文件

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

但是当我定义welcome或其他控制器名称(例如admin)时它无法正常工作

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

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

我的网址是

  

http://localhost/ciDemo/

如果default_controlleradminwelcome,则会有效,default_controllerindex,因此会出现错误

  

未找到404页面

     

找不到您请求的页面。

是否有任何方法可以将index控制器用作default controller

我的控制器文件如下:

Admin
Index
Welcome

2 个答案:

答案 0 :(得分:4)

  

控制器名称

     

由于您的控制器类将扩展主应用程序   控制器你必须小心,不要将你的方法命名为相同的   该类使用的那些,否则你的本地方法会   覆盖他们。以下是保留名称列表。不要说出名字   你的控制器中的任何一个:

     

CI_Controller

     

默认

     

索引

取自codeigniter 3文档

答案 1 :(得分:0)

正如@Dale所说,你不能对控制器使用一些保留关键字。 但如果您仍想使用索引控制器在控制器中添加 __ construct()

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

class Index extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();       
    }

    public function index()
    {
        $this->load->view('welcome_message');
    }
}
相关问题