การดึงค่าคอนฟิกของโค้ทอินิกเตอร์สามารถทำได้โดย ใช้คำสั่ง[code language=”php”]$this->config->item(‘ ตัวแปร ‘);[/code] โดยจะดึงมาจากไฟล์ \application\config\config.php เป็นหลัก แต่ถ้าต้องการแยก config ออกมาเป็นส่วนๆ เพื่อความง่ายในการจัดการ สามารถสร้างไฟล์ ขึ้นมาใน application\config ได้[code language=”php” title=”\application\config\caching.php”]<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
$config[‘caching’][‘adapter’] = ‘memcached’;
$config[‘caching’][‘backup’] = ‘file’;[/code]
จากนั้นเราจะอ่าน configuration เข้ามาในระบบของเราสามารถทำได้ 2 วิธีคือ
- เป็นค่าที่เราต้องใช้บ่อยๆแทบทุกหน้า เพิ่มเข้าไปใน Auto-loading เปิดไฟล์ \application\config\autoload.php เพิ่มชื่อไฟล์ลงไปใน[code language=”php” title=”\application\config\autoload.php”]$autoload[‘config’][/code]เช่น[code language=”php” title=”\application\config\autoload.php”]…
$autoload[‘config’] = [‘caching’];
…[/code] - ถ้าต้องการใช้เฉพาะบางไฟล์ ทำได้โดยการ load ใน controller ที่ต้องการโดยใช้คำสั่ง[code language=”php”]
…
$this->config->load(‘caching’);
…[/code]
ผมใช้หน้าแรกแสดงตัวอย่างการใช้[code language=”php” title=”\application\controllers\Welcome.php”]<?php
defined(‘BASEPATH’) or exit(‘No direct script access allowed’);
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
echo ‘charset = ‘ . $this->config->item(‘charset’);
//$this->config->load(‘caching’);
echo ‘<pre>’, print_r($this->config->item(‘caching’), true), ‘</pre>’;
}
…[/code]
อ่านเพิ่มเติม
About the author