日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【CodeIgniter 】解惑

發(fā)布時間:2025/3/20 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeIgniter 】解惑 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 1. 新建`Main.php`控制器,定義一個`function index(){ echo 'main'; }`, 訪問竟然報錯:`500 Internal Server Error `,很是難受
  • 2. 區(qū)分 `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. 當(dāng)在生產(chǎn)環(huán)境遇到"500 Internal Server Error"后,開啟調(diào)試模式(一直想通過源碼,一點點的調(diào)試)

1. 新建Main.php控制器,定義一個function index(){ echo 'main'; }, 訪問竟然報錯:500 Internal Server Error,很是難受

``` 解決方案:在根目錄下放入 重寫(.htaccess) 文件,文件內(nèi)容如下: Options +FollowSymLinksRewriteEngine on RewriteRule ^(.*)/index/?$ $1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]```

2. 區(qū)分 init() 和 __construct() (參考:https://my.oschina.net/solate/blog/733518)

之前一直以為 init() 是個高大上的函數(shù),查閱之后才發(fā)現(xiàn) `一般都是自己定義的,可以隨便寫名字,一般會在__construct() 中寫 調(diào)用。功能和__construct()差不多但是需要自己寫調(diào)用, 其實就是普通方法,只是這個方法大家一般默認(rèn)都是用來初始化東西的。` __construct():`是PHP內(nèi)置的構(gòu)造函數(shù),實例化之前 PHP 解析引擎自動調(diào)用,做一些初始化的工作或者外部服務(wù)器檢測的工作。在實例化對象之前需要做的工作都寫在這里`

3. 控制器多級目錄的問題

類似這樣 ` application/controller/admin/main.php` ` application/controller/home/main.php` 的目錄,``` 設(shè)置路由 $route['default_controller'] = 'home/main';(默認(rèn)路由出現(xiàn)問題) # 類名/方法名,而不是 路徑/類名 $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行)原內(nèi)容: // 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 問題

在第一個問題中,我們已經(jīng)寫入了重寫文件.htaccess ,但有時 仍然會報錯 404 Not Found , 這就很是難受,
解決方案:

``` 檢查是否安裝有 mod_rewrite 模塊 檢查是否允許 AllowOverride All , 該選項默認(rèn)為 AllowOverride None ```

7. 當(dāng)在生產(chǎn)環(huán)境遇到"500 Internal Server Error"后,開啟調(diào)試模式(一直想通過源碼,一點點的調(diào)試)

``` ini_set('display_errors','on'); error_reporting(E_ALL); ```

遇到問題,再來更新

總結(jié)

以上是生活随笔為你收集整理的【CodeIgniter 】解惑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。