[精讚] [會員登入]
1270

[PHP] CodeIgniter 3+pure+smarty安裝及環境設置1/2

PHP framework CodeIgniter 3+ pure CSS +smarty Template Engine的整合

分享此文連結 //n.sfs.tw/10508

分享連結 [PHP] CodeIgniter 3+pure+smarty安裝及環境設置1/2@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2020-01-14 14:27:26 最後編修
2016-12-29 14:06:31 By 張○○
 

自動目錄

Codeigniter 3: php 的framework,約有14%[2]。以下簡寫CI3,最後更新3.1.5 Jun 19, 2017
smarty 3: 樣版引擎,最後版本 Smarty 3.1.30 Released Aug 14, 2016[3]
pure: css framework[4]

以上選擇,是因為我喜歡簡單的東西。文章共分兩部分,第一部分是smarty3+CI3 結合及測試;第二部分是 pure加入後的整合。

以下範例的下載檔請依你安裝時的版本操作。

一、下載和安裝CI3

CI3 目前已放在github,可以使用github方式安裝,或是直接下載解壓:

到安裝的目錄下,下載及解壓[1]
$ cd /path/to
$ wget http://codeigniter.org.tw/downloads/file/CodeIgniter_3.1.5
$ unzip CodeIgniter_3.1.5

如果路徑權限等設定正確,開啟頁面就能看到ci

二、下載和安裝smarty3

smarty已經改到github,可以使用git的方法來安裝,或直接下載:
$ cd /path/to/application/libraries
$ wget https://github.com/smarty-php/smarty/archive/v3.1.27.zip
$ unzip v3.1.27.zip
$ rm v3.1.27.zip

設定smarty寫入目錄
$ cd /path/to/application/cache
$ mkdir scache
$ mkdir templates_c

設定寫入權限
$ chmod 777 scache/
$ chmod 777 templates_c/

給予SELINUX權限如果有需要的話
# chcon -R -t httpd_sys_rw_content_t scache/
# chcon -R -t httpd_sys_rw_content_t templates_c/

三、設置Smarty3和CI3結合及測試

1. 目前 application/libraries 下已有一個 smarty-3.1.27的目錄,我在application/libraries寫一個Base.php的自寫library:

FILE ./application/libraries/Base.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Base {
  var $v;
  public function __construct()
  {
    include  APPPATH. "libraries/smarty-3.1.27/libs/Smarty.class.php";
    $view = new Smarty();
    // 設定 Smarty 參數
    $view->left_delimiter = '{{';
    $view->right_delimiter = '}}';
    $view->setTemplateDir(APPPATH . 'views');
    $view->setCompileDir(APPPATH . 'cache/templates_c');
    $view->setCacheDir(APPPATH . 'cache/scache');
//    $view->setConfigDir(APPPATH . 'views/config');
    $view->compile_check = true;
    $view->force_compile = true;
    $view->caching = false;
    $view->cache_lifetime = 86400;
    $this->v= $view;
  }
}

2. 接下來新增 Index這個controller

FILE ./application/controllers/Index.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Index extends CI_Controller {
  public function __construct() {
    parent::__construct();
    $this->load->library('base');
  }
 
  public function index()
  {
    $this->base->v->assign('t','TEST');
    $this->base->v->display("index/Test.mtpl");
  }
}

3. 然後增加一個樣版檔 Test.mtpl

FILE ./application/views/index/Test.mtpl

<html>
<body>
this is a
{{$t}}
 
</body>
</html>

4. 最後我修改了預設的config/routes.php, 開瀏覽器測試,一切OK了

$route['default_controller'] = 'index';

顯示結果:
this is a TEST

目前的檔案目錄結構是這樣的:

.
├── cache
│   ├── index.html
│   ├── scache
│   └── templates_c

├── config
├── controllers
│   ├── index.html
│   ├── Index.php
│   └── Welcome.php
├── core
├── helpers
├── hooks
├── index.html
├── language
├── libraries
│   ├── Base.php
│   ├── index.html
│   └── smarty-3.1.27
├── logs
├── models
├── third_party
└── views
    ├── errors
    ├── index
    │   └── Test.mtpl

    └── index.html

接下來下一篇來整合Pure CSS

參考資料

[1] http://codeigniter.org.tw/downloads/

[2] http://beebom.com/2015/02/best-free-php-frameworks

[3] smarty http://www.smarty.net/

[4] PURE css fw http://purecss.io/

END

你可能感興趣的文章

[PHP] preg_match 的貪婪和不貪婪比對 在php preg_match中預設是採用貪婪比對,太貪婪反而不符合需要,因此得採用「非貪婪比對」...

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper

[PHP+ci4] codeignitor4 Cache 及 Session 使用 memcached php 的framework codeignitor4 中的cache和session使用memcached

[PHP]解決ksort新增的SORT_NATURAL|SORT_FLAG_CASE方法 php>=5.4中ksort函數多了SORT_NATURAL 和 SORT_FLAG_CASE 旗標,對舊版的PHP中要怎麼辦?

[PHP] 使用browscap檢查瀏覽器版本 使用PHP內建函數browscap檢查瀏覽器版本

[PHP] 處理mail 函式的標題中文字亂碼 PHP的mail函式若標題有中文字得先處理過,否則收信者會看到亂碼

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

UTF-8的網頁但IE8一片空白 UTF8編碼的網頁在Firefox 正常顯示、但IE8 就是空白,IE8編碼設定是「自動偵測」可是自動偵測到的是 big5...

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI