【CodeIgniter 】解惑
生活随笔
收集整理的這篇文章主要介紹了
【CodeIgniter 】解惑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 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 +FollowSymLinksRewriteEngine 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 , 這就很是難受,
解決方案:
7. 當在生產環境遇到"500 Internal Server Error"后,開啟調試模式(一直想通過源碼,一點點的調試)
``` ini_set('display_errors','on'); error_reporting(E_ALL); ```遇到問題,再來更新
總結
以上是生活随笔為你收集整理的【CodeIgniter 】解惑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多页面(MPA)开发 VS
- 下一篇: Linux 手动或自动挂载 NTFS 硬