文章目录
- 1. 新建`Main.php`控制器,定义一个`function index(){ echo 'main'; }`, 访问竟然报错:`500 Internal Server Error `,很是难受
- 2. 区分 `init()` 和 `__construct()` (参考:https://my.oschina.net/solate/blog/733518)
- 3. 控制器多级目录的问题
- 4. 报错提示:`Unable to locate the model you have specified:*_model`
- 5. 报错信息: 在引入session类库时,报错 `Session: Configured save path '' is not a directory, doesn't exist or cannot be created.`
- 6. 重写失败 `404 Not Found` 问题
- 7. 当在生产环境遇到"500 Internal Server Error"后,开启调试模式(一直想通过源码,一点点的调试)
1. 新建Main.php
控制器,定义一个function index(){ echo 'main'; }
, 访问竟然报错:500 Internal Server Error
,很是难受
```
解决方案:在根目录下放入 重写(.htaccess) 文件,文件内容如下:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
```
2. 区分 init()
和 __construct()
(参考:https://my.oschina.net/solate/blog/733518)
之前一直以为 init() 是个高大上的函数,查阅之后才发现 `一般都是自己定义的,可以随便写名字,一般会在__construct() 中写 调用。功能和__construct()差不多但是需要自己写调用, 其实就是普通方法,只是这个方法大家一般默认都是用来初始化东西的。`
__construct():`是PHP内置的构造函数,实例化之前 PHP 解析引擎自动调用,做一些初始化的工作或者外部服务器检测的工作。在实例化对象之前需要做的工作都写在这里`
3. 控制器多级目录的问题
类似这样 ` application/controller/admin/main.php` ` application/controller/home/main.php` 的目录,
```
设置路由
$route['default_controller'] = 'home/main';(默认路由出现问题) # 类名/方法名,而不是 路径/类名
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['main'] = 'home/main';
$route['news'] = 'home/news';
$route['news/(:any)'] = 'home/news/$1';
$route['admin'] = 'admin/main';
$route['admin/news/(:any)'] = 'admin/news/$1';
```
```
解决方案:(在system/core/Router.php 303行)
原内容:
// 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] . '/';
}
}
```
4. 报错提示:Unable to locate the model you have specified:*_model
解决方案:将model文件名称改为首字母大写,比如 news_model --> News_model
5. 报错信息: 在引入session类库时,报错 Session: Configured save path '' is not a directory, doesn't exist or cannot be created.
解决方案: 在application/config/config.php
中 $config['sess_save_path'] = null;
修改为$config['sess_save_path'] = '/tmp';
6. 重写失败 404 Not Found
问题
在第一个问题中,我们已经写入了重写文件.htaccess
,但有时 仍然会报错 404 Not Found
, 这就很是难受,
解决方案:
```
检查是否安装有 mod_rewrite 模块
检查是否允许 AllowOverride All , 该选项默认为 AllowOverride None
```
7. 当在生产环境遇到"500 Internal Server Error"后,开启调试模式(一直想通过源码,一点点的调试)
```
ini_set('display_errors','on');
error_reporting(E_ALL);
```
遇到问题,再来更新