CodeIgniter环境搭建及默认访问控制器的变动

环境搭建

  1. 使用XAMPP快速搭建环境
  2. CodeIgniter官网下载压缩包(当前最新版本3.1.9)
  3. 将下载的压缩包解压更名为ci_news,并放置在 C:/xampp/htdocs/dashboard/ 文件夹下,如须要变动访问文件夹,请阅读步骤4,如不变动,请阅读步骤5
  4. 变动访问配置文件在 C:/xampp/apache/conf/httpd.conf 文件,将 C:/xampp/htdocs 改成自定义地址
  5. 访问 http://localhost/dashboard/ci_news/ 如图即框架搭建成功

框架目录结构

此处仅做简单描述,index.php为入口文件,system为框架代码,编辑的代码大多写到application文件夹中,其中models为模型,views为视图,controllers为控制器,config为配置文件(后续待补) php

默认访问控制器的变动

如图为默认访问的控制器 apache

但在实际项目中,为便于维护,需将php文件放入不一样文件夹中,例如将controllers文件夹下新建index和adamin先后台两个文件夹,在index文件夹中新建Home.php文件,因而将

$route['default_controller'] = 'welcome';
复制代码

改成app

$route['default_controller'] = 'index/home';
复制代码

但访问 http://localhost/dashboard/ci_news/ 时报404,经发现,3.1.4版本以后不支持这样的修改,在system/core/Router.php文件下找到 _set_default_controller 方法框架

if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
{
    This will trigger 404 later
    return;
}
复制代码

将这一段改成codeigniter

if ( ! file_exists(APPPATH . 'controllers/' . $this->directory . ucfirst($class) . '.php'))
{
    $path_arr = explode('/', trim($this->default_controller, '/'));
    
    $class = ucfirst($path_arr[1]);
    $method = isset($path_arr[2]) ? $path_arr[2]: 'index';
    
    if (file_exists(APPPATH . 'controllers/' . $this->directory . $path_arr[0]. '/' . $class . '.php'))
    {
        $this->directory .= $path_arr[0]. '/';
    }
}
复制代码

从新访问 http://localhost/dashboard/ci_news/this

相关文章
相关标签/搜索