CodeIgniter 3 — 環境安裝

Yu-Cheng Hung
7 min readNov 26, 2019

--

目前筆者主要的任務目標是將以PHP CodeIgniter framework撰寫的舊專案改用PHP Laravel 6 framework來撰寫,因此勢必要先理解CodeIgniter的架構以利後續作業。除了學習如何架設CodeIgniter 3環境外,也經由前輩建議實作一個簡單的CodeIgniter動態頁面來更快速了解其運作原理,那麼今天就先來看看如何安裝CodeIgniter 3吧!

這邊筆者是在Windows 10中以XAMPP架設環境,再以Composer安裝CodeIgniter 3,有關XAMPP和Composer的安裝及使用方式可參考前篇文章 — Laravel 6 之環境安裝與注意事項

使用Composer下載CodeIgniter 3

打開cmd並且cd C:\xampp\htdocs,輸入下列指令下載並創建一個名稱為ci的CodeIgniter 3專案。

下載完成後htdocs資料夾內的確出現一個ci專案

打開瀏覽器查看 http://localhost/ci

看到上圖畫面恭喜你已成功創建一個CodeIgniter 3專案!

CodeIgniter 3相關設定

首先開啟 application/config/config.php,分別修改下列三行程式碼:

$config['index_page'] = '';

$config['composer_autoload'] = 'vendor/autoload.php';

$config['encryption_key'] = '填入任意亂碼,這是給 Encryption Class 加密用';

接著修改application/config/autoload.php:

$autoload['libraries'] = array('database');    //把database啟用

$autoload['helper'] = array('url');

到這邊,我們重新整理剛剛的http://localhost/ci,會出現以下錯誤訊息:

原因是我們沒有設定database讓CodeIgniter去自動載入,因此需執行以下的步驟。

在phpMyAdmin中建置database

http://localhost/phpmyadmin 進入XAMPP MySQL,預設有一個空的database -> test,我們將在它裡面建立第一張table -> news

CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
slug varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
KEY slug (slug)
);

接著insert兩筆資料進入news中:

INSERT INTO `news` (`id`, `title`, `slug`, `text`) VALUES  (1,'Write Like You Talk','first','Heres a simple trick for getting more people to read what you write: write in spoken language. Something comes over most people when they start writing. They write in a different language than they'),  (2,'A decade at google','second','One of the key challenges you face in an industrial research lab is how to choose your projects. You want your projects to be interesting research but also contribute to your company. As a junior researcher, you?re typically in the situation of choosing a project to join, while later in your career you are expected to come up with and lead your own projects. Regardless of your age, you have to make an educated decision.');

接著回到 application/config/database.php,XAMPP預設username是root

如此即可讓test這個database被正確載入!

相關設定到這邊基本上已經差不多了,最後有一個關於網址的設定。

CodeIgniter的網址一般來說會長得像這樣:

http://example.com/[控制器類別]/[控制器方法]/[參數]

控制類別:指繼承Controller的Class
控制方法:指Controller裡頭的function
參數:傳入function的參數

而CodeIgniter的Model、Controller、View正好對應放置於 application的models、controllers、views資料夾中。

根據上述規則,讓我們試著執行 http://localhost/ci/welcome/index

但結果……

看了CodeIgniter使用手冊,加入index.php於控制器類別前,再執行一次:http://localhost/ci/index.php/welcome/index

成功了!可是這樣的網址看起來就沒那麼簡潔直覺,有沒有方法可以讓index.php消失呢?

有的!讓我們在專案的根目錄中新增一個檔案 .htaccess ,並貼入下列程式碼, 指定不用 Rewrite 的檔案或路徑名稱。

DirectoryIndex index.phpRewriteEngine onRewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

如此一來不管是 http://localhost/ci/index.php/welcome/index 或是 http://localhost/ci/welcome/index 都能顯示出正確的畫面了!

最後修改一下根目錄中的index.php

這樣就完成啦!下一篇我們會來實作一個簡單的CodeIgniter動態頁面。

更多資訊請參考:

CodeIgniter繁體中文官方網站

CodeIgniter官方使用手冊

--

--

No responses yet