环境搭建
- 使用XAMPP快速搭建环境
- 到CodeIgniter官网下载压缩包(当前最新版本3.1.9)
- 将下载的压缩包解压改名为ci_news,并放置在 C:/xampp/htdocs/dashboard/ 文件夹下,如需要变更访问文件夹,请阅读步骤4,如不变更,请阅读步骤5
- 变更访问配置文件在 C:/xampp/apache/conf/httpd.conf 文件,将 C:/xampp/htdocs 改为自定义地址
- 访问 http://localhost/dashboard/ci_news/ 如图即框架搭建成功
框架目录结构
此处仅作简单描述,index.php为入口文件,system为框架代码,编辑的代码大多写到application文件夹中,其中models为模型,views为视图,controllers为控制器,config为配置文件(后续待补)
默认访问控制器的变更
如图为默认访问的控制器
但在实际项目中,为便于维护,需将php文件放入不同文件夹中,例如将controllers文件夹下新建index和adamin前后台两个文件夹,在index文件夹中新建Home.php文件,于是将$route['default_controller'] = 'welcome';
复制代码
改为
$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;
}
复制代码
将这一段改为
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]. '/';
}
}
复制代码