การดึงค่าคอนฟิกของโค้ทอินิกเตอร์สามารถทำได้โดย ใช้คำสั่ง
1 | $this ->config->item( ' ตัวแปร ' ); |
โดยจะดึงมาจากไฟล์ \application\config\config.php เป็นหลัก แต่ถ้าต้องการแยก config ออกมาเป็นส่วนๆ เพื่อความง่ายในการจัดการ สามารถสร้างไฟล์ ขึ้นมาใน application\config ได้
1 2 3 4 5 | <?php defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); $config [ 'caching' ][ 'adapter' ] = 'memcached' ; $config [ 'caching' ][ 'backup' ] = 'file' ; |
จากนั้นเราจะอ่าน configuration เข้ามาในระบบของเราสามารถทำได้ 2 วิธีคือ
- เป็นค่าที่เราต้องใช้บ่อยๆแทบทุกหน้า เพิ่มเข้าไปใน Auto-loading เปิดไฟล์ \application\config\autoload.php เพิ่มชื่อไฟล์ลงไปใน
\application\config\autoload.php 1$autoload
[
'config'
]
เช่น
\application\config\autoload.php 123...
$autoload
[
'config'
] = [
'caching'
];
...
- ถ้าต้องการใช้เฉพาะบางไฟล์ ทำได้โดยการ load ใน controller ที่ต้องการโดยใช้คำสั่ง
123
...
$this
->config->load(
'caching'
);
...
ผมใช้หน้าแรกแสดงตัวอย่างการใช้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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>' ; } ... |
อ่านเพิ่มเติม
About the author