Welcome Guest ( Log In | Register )

21 Pages V   1 2 3 > » 

สร้าง Virtual Host ใน Apache WebServer

Posted by up1, Jan 21 2010, 04:51 PM in Apache WebServer

การ config virtual host ใน apache webserver สามารถทำได้ดังนี้

1. config/httpd.conf ทำการ include file conf/extra/httpd-vhosts.conf ดังนี้

QUOTE
Include onf/extra/httpd-vhosts.conf


2. ทำการ config ใน file conf/extra/httpd-vhosts.conf เพื่อเพิ่ม virtual host ที่เราต้องการสร้าง ดังนี้

CODE
<VirtualHost *:80>
    ServerAdmin admin@xx.com
    DocumentRoot "C:/AppServ/www/my"
    ServerName my.xxx.com
    ErrorLog "logs/my-error.log"
    CustomLog "logs/my-access.log" common
</VirtualHost>

<Directory "C:/AppServ/www/my">
  Order Deny,Allow
  Allow from all
</Directory>


คำอธิบาย
DocumentRoot คือ folder root ของ project
ServerName คือ ชื่อของ virtual ที่เราต้องการสร้าง
ErrorLogม CustomLog คือ log files ต่างๆ ซึ่งจะแยกตาม virtual host


ในการใช้งานในเครื่อง dev บน windows แบบง่ายๆ ก็ไป config ใน file hosts ได้เลย เช่น
QUOTE
127.0.0.1 my.xxx.com


ถ้าบน linux ก็ไปกำหนดใน file /etc/hosts นะครับ


----- เพิ่มเติม ------
วันนี้เจอปัญหาบน server คือไม่สามารถ access ได้โดยตรง ซึ่งพบว่า ใน file httpd.conf นั้นไม่ได้ allow ในการ access directory ไว้ ดังนั้นวิธีการแก้ไขคือ
- ใน file conf/httpd.conf นั้นกำหนด AllowOverride All
หรือ
- ใน file conf/extra/httpd-vhosts.conf กำหนด AllowOverride All ในแต่ละ virtual host ที่กำหนด


CodeIgniter :: มาทำความรู้จัก Active Record Class กัน

Posted by up1, Jan 19 2010, 12:14 AM in CodeIgniter

มาทำความรู้จัก Active Record Class กัน
ที่มา :: Active Record Class

ในส่วน ของ model class ของ CodeIgniter นั้นสามารถ mapping table ใน database เข้าได้ โดยใช้แนวคิดของ Active Record Pattern หรืออาจจะเรียกว่า CodeIgniter นั้นมี OR-Mapping มาให้ด้วย ซึ่งทำให้การจัดการข้อมูลใน database ง่ายและสะดวกมากยิ่งขึ้น รวมทั้งยังไม่ผูกติดกับ database ด้วย

สิ่งที่เตรียมไว้ใน Active Record Class มีดังนี้

QUOTE
- Select Data
- Insert Data
- Update Data
- Delete Data
- Method Chaining
- Active Record Caching


ปล. ในบทความนี้ใช้ MySQL นะครับ


- Select Data ดึงข้อมูล
จะทำการสร้าง select statement มาให้เอง ผ่านการใช้ method/function เหล่านี้

:: $this->db->get( $table_name ); ::
เป็นการดึงข้อมูลทั้งหมด จาก table ที่กำหนด

ตัวอย่างการใช้งาน
$query = $this->db->get( 'blog' );

sql statement ที่สร้างขึ้นมาคือ select * from blog

:: $this->db->get( $table_name, $limit, $offset ); ::
เป็นการดึงข้อมูลแบบ paging จาก table ที่กำหนด

ตัวอย่างการใช้งาน
$query = $this->db->get( 'blog', 10, 20 );

sql statement ที่สร้างขึ้นมาคือ select * from blog LIMIT 20, 10


:: $this->db->get_where( $table_name, array(), $limit, $offset ); ::
เป็นการดึงข้อมูลด้วยแบบ paging จาก table ที่กำหนด

ตัวอย่างการใช้งาน
$query = $this->db->get_where('blog', array('id' => 1), 10, 20);

sql statement ที่สร้างขึ้นมาคือ
select * from blog where id = 1 LIMIT 20, 10

:: $this->db->select(); ::
สามารถเลือก columns ที่ต้องการได้

ตัวอย่างการใช้งาน
this->db->select('title, content, date');
$query = $this->db->get('blog);

sql statement ที่สร้างขึ้นมาคือ
select title, content, date from blog


:: $this->db->from(); ::
สร้าง from ใน sql statement ซึ่งจะต้องใช้งานร่วมกับ $this->db->get()

ตัวอย่างการใช้งาน
$this->db->select('title, content, date');
$this->db->from('blog');
$query = $this->db->get();

sql statement ที่สร้างขึ้นมาคือ
select title, content, date from blog



...... พักก่อน .... มันเยอะจริงๆ


Link :: Web Security: Are You Part Of The Problem?

Posted by up1, Jan 18 2010, 03:26 AM in Web Technologies

ที่มา :: Web Security: Are You Part Of The Problem?


CodeIgniter :: เริ่มต้นเกี่ยวกับ Model

Posted by up1, Jan 18 2010, 02:17 AM in CodeIgniter

ที่มา :: http://codeigniter.com/user_guide/general/models.html

Model คืออะไร
- คือ M ใน MVC model
- ใน CodeIgniter คือ PHP classes ที่ถูกออกแบบให้ทำงานกับข้อมูลที่อยู่ใน database
- ในส่วนของ Model ของ CodeIgniter นั้นจะใช้ Active Record Pattern มาช่วยเรื่องการจัดการข้อมูลต่างๆ ใน database ด้วยการเขียน script สั้นๆ

ตัวอย่างเช่น ระบบ blog ใน Model ประกอบไปด้วย functions ต่างๆ เช่น insert, update, get ข้อมูลของ
blog จาก database ดังนี้

CODE
class Blogmodel extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function Blogmodel()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}



โครงสร้างของ Model
- Model classes จะถูกเก็บไว้ใน folder /system/application/models และสามารถมี folder ย่อยๆ ได้
- Model จะมีหน้าตาดังนี้

CODE
class Model_name extends Model {

    function Model_name()
    {
        parent::Model();
    }
}


- ชื่อของ Model จะต้องขึ้นต้นด้วยอักษรตัวใหญ่ และต้อง extends จาก Model class หรือ sub-class ของ Model class

ตัวอย่าง Blog_model

CODE
///system/application/models/user_model.php
class Blog_model extends Model {

    function Blog_model()
    {
        parent::Model();
    }
}



การใช้งาน Model
- manual loading คือ จะทำการ load เมื่อจะใช้งาน ใน Controller class ดังนี้

CODE
$this->load->model('Model_name');
$this->Model_name->function();


หรือถ้าต้องการใช้ชื่อ object อื่น ด้วยการ alias ดังนี้

CODE
$this->load->model('Model_name', 'fubar');
$this->fubar->function();


ตัวอย่างการใช้งานของ Manual Loading ดังนี้

CODE
class Blog_controller extends Controller {

    function blog()
    {
        $this->load->model('Blog');
        $data['query'] = $this->Blog->get_last_ten_entries();
        $this->load->view('blog', $data);
    }
}


- auto loading ทำการ config ไว้ใน file /system/application/config/autoload.php


การติดต่อไปยัง Database
- ทำการ config database parameters ได้ใน file /system/application/config/database.php
- เมื่อทำการ load model ขึ้นมานั้น ระบบจะยังไม่ติดต่อไปยัง database ดังนั้น ถ้าต้องการให้ model ติดต่อไปยัง database สามารถทำได้ดังนี้

CODE
$this->load->model('Model_name', '', TRUE);


- ถ้าต้องการให้ติดต่อ database แบบ automatic สามารถทำการ config ใน file /system/application/config/autoload.php และเพิ่มการ load "database" ในส่วน library ดังนี้

CODE
$autoload['libraries'] = array( 'database' );



เพิ่มเติมเกี่ยวกับการจัดการกับ Database
http://codeigniter.com/user_guide/database/index.html


CodeIgniter :: การทำ Dynamic Configuration

Posted by up1, Jan 15 2010, 06:25 PM in CodeIgniter

CodeIgniter :: Dynamic Configuration

ที่มา ::
Setting up Codeigniter with dynamic configuration variables – fit for development teams

Problem

ในการพัฒนาระบบด้วย CodeIgniter นั้นจะมี environment ประมาณ 2-3 ตัวคือ
- developer
- testing/statging
- production

ดังนั้นเราจะทำอย่างไรกันดี ???

Solution

สิ่งที่จะต้องทำคือ แยกการ configuration ตาม environment ที่กำหนด เช่น development, test, production เป็นต้น โดยค่า configuration ที่ต้องเปลี่ยนไปตาม environment คือ

QUOTE
- ค่า $config['base_url'] คือ ค่า url ของ site ที่พัฒนา อยู่ใน file /system/application/config/config.php
- การปรับแต่ง database ใน file /system/application/config/database.php


หลังจากได้เป้าหมายที่จะทำแล้ว ก็มาเริ่มกันเลย

1. ทำการกำหนดค่าคงที่ ( Constants) ของชนิด environement ที่มีในระบบใน file /system/application/config/constants.php ดังนี้

CODE
define('DEVELOPMENT_SERVER_NAME', 'localhost');
define('TEST_SERVER_NAME', 'http://test.domain.com');
define('STAGING_SERVER_NAME', 'http://statging.domain.com');
define('PRODUCTION_SERVER_NAME', 'http://live.domain.com');


2. จัดการเกี่ยวกับ $config['base_url'] ใน file /system/application/config/config.php ตามแต่ละ environment
โดยดึงค่าจาก server name มันใช้ในการตัดสินใจ


CODE
switch ($_SERVER['SERVER_NAME']) {

   case DEVELOPMENT_SERVER_NAME:
        $config['base_url']    = "http://localhost/";
        break;
   case TEST_SERVER_NAME:
        $config['base_url']    = "http://test.domain.com";
        break;
   case STAGING_SERVER_NAME:
        $config['base_url']    = "http://statging.domain.com";
        break;
   default:
        $config['base_url']    = "http://live.domain.com";
        break;
}



3. จัดการเกี่ยวกับ database ใน file /system/application/config/database.php ตามแต่ละ environment ได้ดังนี้

3.1 จัดการกับ active group ของ database configuration ได้ดังนี้

CODE
switch ($_SERVER['SERVER_NAME']) {

   case DEVELOPMENT_SERVER_NAME:
     $active_group = "local";
     break;
   case TEST_SERVER_NAME:
        $config['base_url']    = "test";
        break;
   case STAGING_SERVER_NAME:
     $active_group = "staging";
     break;
   default:
     $active_group = "production";
     break;

}


และกำหนด configuration ของแต่ละ environment ดังนี้

CODE
$db['local']['hostname'] = "localhost";
$db['local']['username'] = "DEV_USERNAME";
$db['local']['password'] = "DEV_PASSWORD";
$db['local']['database'] = "DEV_DATABASENAME";
$db['local']['dbdriver'] = "mysql";

$db['test']['hostname'] = "localhost";
$db['test']['username'] = "TEST_USERNAME";
$db['test']['password'] = "TEST_PASSWORD";
$db['test']['database'] = "TEST_DATABASENAME";
$db['test']['dbdriver'] = "mysql";

$db['staging]['hostname'] = "localhost";
$db['staging']['username'] = "STAGING_USERNAME";
$db['staging']['password'] = "STAGING_PASSWORD";
$db['staging']['database'] = "STAGING_DATABASENAME";
$db['staging']['dbdriver'] = "mysql";

$db['production']['hostname'] = "localhost";
$db['production']['username'] = "PRODUCTION_USERNAME";
$db['production']['password'] = "PRODUCTION_PASSWORD";
$db['production']['database'] = "PRODUCTION_DATABASENAME";
$db['production']['dbdriver'] = "mysql";


เพียงเท่านี้ก็สามารถทำ Dynamic Configuration ใน CodeIgniter ได้แล้วครับ


CodeIgniter :: การสร้าง Core System Classes

Posted by up1, Jan 14 2010, 10:04 PM in CodeIgniter

จาก entry นี้ CodeIgniter :: ใช้งาน Profiling ในระบบแบบง่ายๆ ทำให้ผมยังงงว่าทำไมต้องตั้งชื่อขึ่นต้นด้วย MY_ ?? และไม่ต้องทำการ load library ที่เขียนขึ้นมาด้วย ???

ดังนั้น จึงต้องไปหาความรู้เพิ่มเติมจาก User Guilde ของ CodeIgniter จึงเห็นหัวข้อนี้ Creating Core System Classes ซึ่งตรงกับความต้องการมาก ดังนั้นเรามาศึกาากันว่าเป็นยังไงกันบ้าง

เริ่มต้นกันเลย ...


เมื่อ CodeIgniter ทำงานนั้นจะทำการ load classes ต่างๆ ขึ้นมามากมายแบบอัตโนมัติ [ Class Loader ใน java ] โดยจะมี core system classes และ classes ที่ผู้พัฒนาทำการ extends มาจาก core system classes

มาดูว่า Core System Classes ประกอบไปด้วยอะไรบ้าง
- Core System Classes คือ classes ที่ทาง CodeIgniter เตรียมไว้ให้ใช้งานแล้ว โดยจะเก็บไว้ใน folder
/system/libraries ดังนี้
- Benchmark
- Config
- Controller
- Exceptions
- Hooks
- Input
- Language
- Loader
- Log
- Output
- Router
- URI


CodeIgniter เปิดให้เราสร้าง classes ไป replace หรือแทนที่ System Core Classes ได้ โดยทำดังนี้
- สร้าง file php ใน folder /system/application/libraries/
- php file ที่สร้างขึ้นมานั้น จะต้องมี class ที่มีชื่อขึ้นต้นด้วยคำว่า CI ดังตัวอย่าง

CODE
//Input.php
class CI_Input {

}


CodeIgniter ก็เปิดให้เราสร้าง SubClass จาก Core Classes เช่นกัน เพื่อเพิ่มความสามารถบางอย่างเข้าไป จะเรียกว่า Extending classes ซึ่งในการ implements จะต้องทำดังนี้
- class ที่สร้างขึ้นจะต้อง extends System Core Classes
- ชื่อของ class และ file ที่สร้างขึ้นนั้นจะต้องขึ้นต้นด้วย MY_ โดยค่านี้จะกำหนดไว้ใน file /system/application/config/config.php ดังนี้

CODE
$config['subclass_prefix'] = 'MY_';


โดยค่าของ $config['subclass_prefix'] ไม่ควรเป็น CI_ เนื่องจากเป็น prefix ของ Core System Classes ซึ่งจะทำให้สับสนได้

ตัวอย่างเช่น การ extens มาจาก Input class จะเป็นดังนี้

CODE
// system/application/libraries/My_Input.php
class MY_Input extends CI_Input {

}


หรือจากตัวอย่างในการ extends Controller มาก็เช่นกัน ทำให้ CodeIgniter นั้นมีความยึดหยุ่นมากครับ




CodeIgniter :: ใช้งาน Profiling ในระบบแบบง่ายๆ

Posted by up1, Jan 14 2010, 06:30 PM in CodeIgniter

Problem
หลังจากที่ดู Profiling ของ CodeIgniter แล้วพบว่ามันใช้งานง่ายมาก เพียงแต่ไปปิดการใช้งานใน method ของ controller ดังนี้

CODE
$this->output->enable_profiler(TRUE);


โดยจะแสดงข้อมูลต่างๆ เช่น
- parameter ที่ส่งมาทั้ง POST และ GET
- เวลาในการทำงานทั้งหมด
- เวลาการดึงข้อมูลจาก model, database รวมทั้ง sql query ที่ใช้ด้วย

แต่ปัญหาที่เจอคือ ต้องนำ code จากข้างต้นใส่ในทุกๆ method ที่ต้องการจะดู profiling ซึ่งไม่มีดีเลย ??? แล้วจะทำอย่างไรดี ???


Solution

วิธีการแก้ปัญหา คือ ใช้ความสามารถของ OOP ในเรื่องของ inheritance มาช่วย และเพิ่มการ config เพื่อเปิดปิดได้ตามต้องการ ดังนี้

1. เพิ่มการ config ใน file /system/application/config/config.php ดังนี้

CODE
$config['debug'] = TRUE;


โดยมีค่าเป็น TRUE หรือ FALSE


2. สร้าง Base Class ของ Controller ขึ้นมาชื่อว่า MY_Controller.php เก้บไว้ที่ folder /system/application/libraries/ ดังนี้

CODE
<?php

class MY_Controller extends Controller {

    function __construct() {
        parent::__construct();
        $this->output->enable_profiler($this->config->item('debug'));
    }

}

?>


คำอธิบาย
ใน constructor จะทำการเปิดหรือปิดการแสดง profiling ตามค่าที่กำหนดไว้ใน config.php

3. การใช้งาน ทำได้ด้วยการที่ทุกๆ controller จะต้อง extends My_Controller เสมอ ดังนี้

CODE
class Guestbook extends MY_Controller {
  ...
  ...
}



เพียงเท่านี้ก็สามารถดูและจัดการ profiling แบบง่ายๆ ของการทำงานในระบบได้แล้ว


CodeIgniter :: สร้าง Memcache Library เอาไว้ใช้งานอย่างง่าย

Posted by up1, Jan 14 2010, 10:16 AM in CodeIgniter

Problem

จะต้องใช้งาน Memcache ในระบบ จะต้องทำอย่างไรดี ให้ใช้งานง่าย เข้าใจง่าย และ config ได้ง่าย ??

Solution

ในระบบจำเป็นจะต้องใช้ Memcache สำหรับทำ caching ของข้อมูลต่างๆ ภายในระบบงานที่พัฒนาด้วย CodeIgniter แต่ในตัว core ของ CodeIgniter ดันไม่มี library หรือ helper ให้ใช้ [ ทำไมไม่ทำมาให้ ?? ] ดังนั้นจึงค้นหาข้อมูลว่ามีใครทำ library หรือ helper อะไรไว้ให้ใช้บ้างหรือไม่ ผลปรากฎว่ามีเยอะแยะมากมายตามแต่ละคนจะต้องการ แต่ยังไม่โดนใจสักเท่าไร ดังนั้น จึงคิดว่างั้นลองมาเขียนใช้เองบ้างดีกว่า โดยมีขั้นตอนการพัฒนาดังนี้

1. คิดเสียก่อนว่าจะสร้างอะไร อย่างไร เพื่อให้ใช้งานง่ายและมีความยึดหยุ่นพอประมาณ ดังนั้นจึงวางแผนก่อน coding ไว้ดังนี้
- จะสร้างเป็น Library เนื่องจากสามารถนำมาใช้งานง่าย และ เข้าใจง่าย
- ทำการ config พวก Memcache Servers ได้ใน file /system/application/config/config.php เพื่อความยึดหยุนของระบบ ซึ่งจะมีการ config ดังนี้

CODE
$config['use_memcache'] = true;
$config['memcache_servers'] = array (
    array (
        'host' => 'host1',
        'port' => '11211'
    ),
    array (
        'host' => 'host2',
        'port' => '11211'
    )
);

คำอธิบาย
- $config['use_memcache'] คือ กำหนดว่าจะใช้งาน memcache ในระบบหรือไม่ มีค่า true กับ false
- $config['memcache_servers'] คือ กำหนด Memcache Server และ port ที่เปิดใช้งาน โดยสามารถกำหนดได้มากกว่า 1 เครื่อง


2. ทำการสร้าง Memcache Library ในระบบ โดยสร้าง file Memcache.php ใน folder /system/application/libraries/ โดยประกอบไปด้วย methods ดังนี้
- connect
- set
- get
- delete

โดย code จะเป็นดังนี้
CODE
class CI_Memcache {

    function CI_Memcache() {
        $this->CI = & get_instance();
        $this->connect();
    }

    /**
     * Connect to memcache server
     */
    function connect() {
        $this->memcache = new Memcache;
        $this->connected_servers = array ();
        if ($this->CI->config->item('use_memcache')) {
            $servers = $this->CI->config->item('memcache_servers');
            if (!empty ($servers)) {
                foreach ($servers as $server) {    
                    if ($this->memcache->addServer($server['host'], $server['port'])) {
                        $this->connected_servers[] = $server;
                    }
                }
            }
        }
    }

    /**
     * Set data
     */
    function set($key, $value, $expire = 600) {
        if (empty ($this->connected_servers)) {
            return false;
        } else {
            return $this->memcache->set($key, $value, 0, $expire);
        }
    }

    /**
     * Get data by key
     */
    function get($key) {
        if (empty ($this->connected_servers)) {
            return false;
        } else {
            return $this->memcache->get($key);
        }
    }

    /**
     * Delete data by key
     */
    function delete($key, $timeout = 0) {
        if (empty ($this->connected_servers)) {
            return false;
        } else {
            return $this->memcache->delete($key, $timeout);
        }
    }

}


คำอธิบาย
ส่วนที่น่าสนใจคือ การดึงค่าจาก config.php ที่เรากำหนดไว้นั่นเอง จะอยู่ในส่วนนี้

CODE
$this->CI = & get_instance();

$this->CI->config->item('use_memcache');

$servers = $this->CI->config->item('memcache_servers');


3. การใช้งาน เป็นดังนี้

- ทำ auto load memcache library ใน file /system/application/config/autoload.php ดังนี้

CODE
$autoload['libraries'] = array( 'memcache' );


หรือทำการ load ก่อนใช้งานดังนี้

CODE
$this->load->library('memcache');


- ทำการ set/get/delete ข้อมูลใน memcache ดังนี้

CODE
if( !$this->memcache->set( 'test1', 'data'. rand() )) {
   echo "error from set";
}

if( !$this->memcache->get( 'test1' )) {
   echo "error from get";
}

if( !$this->memcache->delete( 'test1' )) {
   echo "error from delete";
}


เพียงเท่านี้ก้สามารถสร้าง Memcache Library ใน CodeIgniter อย่างง่ายได้แล้วครับ


CodeIgniter :: การสร้าง Library เอาไว้ใช้งาน

Posted by up1, Jan 13 2010, 01:45 PM in CodeIgniter

Problem

QUOTE
ถ้าต้องการเพิ่มความสามารถต่างๆ เข้าไปในระบบงานที่พัฒนาด้วย CodeIgniter เช่น
- การดึงข้อมูลจาก service ในรูปแบบของ xml, json, xml-rpc
- การใช้งานพวก caching เช่น memcache

จะทำอย่างไรดี ให้สามารถใช้งานได้ง่าย เข้าใจง่าย และ reuse ได้ด้วย ???


Solution

ความคิดแรกคือ ทำเป็น Helper สิ ง่ายดาย เพียงแค่เขียนเป็น method ที่จำเป็นต่อการใช้งานไป ก็เป็นอันเสร็จสิ้น

แต่

เมื่อทำการ implement ดูและนำมาใช้งานพบว่า คนใช้งานเองก็งงว่า method นี้มันคืออะไร อยู่ที่ไหน ไม่มีที่มาที่ไป การ maintain code ก็ยากขึ้นมาตามตัวทันที ดังนั้นจึงคิดว่า เอาไปทำเป็น library กันดีกว่า


ดังนั้นจึงทำเป็น Library โดยสร้าง file ไว้ที่ /system/application/libraries/ เลย และเมื่อต้องการใช้งานก็จัดการที่ส่วน /system/application/config/autoload.php ได้เลย

ส่วนการใช้งานเป็นดังนี้
CODE
$this->library_name->method_name( parameter );


ซึ่งจากการใช้งานทำให้ที่มาที่ไปของ method นั้นๆ ชัดเจนกว่า helper มาก


ส่วน helper จะนำไปใช้ช่วย view ซะมากกว่า .....


... ทั้งหมดสรุปสั้นๆ จากการศึกษาไม่กี่วันนะครับ .... ถูกผิดแนะนำกันได้ ......
smile.gif smile.gif smile.gif smile.gif







CodeIgniter :: จัดการ css, image, javascript ด้วย Asset Helper

Posted by up1, Jan 12 2010, 12:05 PM in CodeIgniter

มีคำถามขึ้นมาว่า

QUOTE
เราจะทำการจัดการเกี่ยวกับ file พวก css, javascript, image กันอย่างไรใน CodeIgniter ??


เมื่อไปลองค้นหาดูพบว่ามี 2 วิธีที่น่าสนใจคือ

1. ใช้สิ่งที่มีมากับ core ของ CodeIgniter เลยคือการใช้ url helper มาช่วย เพื่อดึงค่าของ base url ของระบบ แ้ล้วต่อด้วย path และ ชื่อ file ไปเอาเช่น

CODE
<link href="<?=base_url()?>assets/modules/module1/css/test.css" rel="stylesheet" type="text/css" />


ถ้าไม่สามารถใช้งาน function base_url() ได้ก็ให้ทำการ load url helper มาด้วย ซึ่งทำได้ 2 วิธีคือ
- กำหนดใน /system/application/config/autoload.php ดังนี้

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


- กำหนดใน code ไปเลย ดังนี้่

CODE
$this->load->helper('url');



แต่วิธีการเช่นนี้อาจจะใช้กำลังภายในเยอะหน่อย เนื่องจากจะต้องไปแทรก code php ไว้ตลอด ดังนั้นมาดูอีกวิธี

2. ใช้ Asset Helper เข้ามาช่วย โดยโครงสร้างของ folder จะเป็นดังรูป

Attached Image


คำอธิบาย
- folder css, image, js ที่อยู่ root node นั้นจะเป็น default ของระบบ
- ส่วน folder css, image, js ที่อยู่ใน modules นั้นจะแยกไปตาม module หรือระบบย่อยๆ ซึ่งทำให้สามารถจัดการกับ resources ต่างๆ เหล่านี้ให้เป็นระเบียบมากยิ่งขึ้น

การใช้งาน Asset Helper เป็นดังนี้

1. ทำการ Download Asset Helper แล้วทำการ extract และ copy file asset_helper.php ไปที่ folder /system/application/helpers/

2. แก้ไข file .htaccess ดังนี้

QUOTE
RewriteEngine on
RewriteRule ^$ index.php [L]
RewriteCond $1 !^(index\.php|images|css|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]


โดยเพิ่ม assets เข้ามาเพื่อให้ตัว Mod Rewrite มันทำงานถูกต้อง [ สามารถเปลี่ยนแปลงได้ตาม code ที่กำหนดไว้ใน file /system/application/helpers/asset_helper.php ]

3. ทำการ load asset helper แบบ auto ใน file /system/application/config/autoload.php ดังนี้

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


หรือจะ load ใน code ก่อนใช้งานก็ได้ ดังนี้

CODE
$this->load->helper('asset');


4. การใช้งาน asset สามารถใช้ได้ทั้งใน controller, view และ template ดังนี้

:: การใช้งานใน template ::

- ใช้งาน css ที่เป็น default
CODE
echo css_asset('test.css');


ผลลัพธ์ที่ได้คือ
QUOTE
<link href="http://localhost/CodeIgniter/assets/css/test.css" rel="stylesheet" type="text/css" />


- ใช้งาน css ที่เป็นอยู่ใน module1
CODE
echo css_asset('test.css', 'module1');


ผลลัพธ์ที่ได้คือ
QUOTE
<link href="http://localhost/CodeIgniter/assets/modules/module1/css/test.css" rel="stylesheet" type="text/css" />


- ใช้งาน image
CODE
image_asset('filename.jpg', 'modulename', array('alt'=>'Image name!', 'width'=>50));


- ใช้งาน javascript
CODE
js_asset_url('filename.js', 'modulename');


- ใช้งาน resources อื่นๆ เช่น flash โดยจากตัวอย่างจะเก็บไว้ใน folder ชื่อว่า flash
CODE
other_asset_url('banner.swf', '', 'flash');



นี่คือวิธีจัดการกับ resources ต่างๆ ในระบบของ CodeIgniter จะใช้วิธีไหนก็แล้วแต่ความเหมาะสมนะครับ


21 Pages V   1 2 3 > »   

« February 2010 »

SMTWTFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Search My Blog