ข้ามไปที่เนื้อหาหลัก

วิธีสร้าง Dynamically Controllers ใน CodeIgniter Framework

How to create dynamically controllers function name in codeigniter.

1. แก้ไขไฟล์คอนฟิก application/config/routes.php

$route['product/(:any)'] = 'product/show/$1';

2. สร้างคอนโทรลเลอร์ที่ชื่อ product เอาไว้รับผ่านฟังก์ชั่น show

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product extends MY_Controller {

/**
* Index Page for this controller.
*/
public function show($category='')
{
    echo $category;//อยากได้ข้อมูลอะไรก็เอาค่าที่ส่งมาไปค้นหา
}

}
3. ตัวอย่างเช่น
     http://localhost/my_project/index.php/product/new  => สินค้าใหม่
     http://localhost/my_project/index.php/product/hdd  => ฮาร์ดดิสก์
     http://localhost/my_project/index.php/product/ram  => แรม

สังเกตว่าเวลาลิงค์ไปหน้า Product ไม่จำเป็นต้องสร้างฟังก์ชั่น new(), hdd(), ram() แต่เราจะวิ่งไปที่ฟังก์ชั่น show() ตามที่กำหนดไว้ในไฟล์ routes.php


ที่มา : https://expressionengine.com/forums/archive/topic/227841/building-pages-dynamically-from-a-database

ความคิดเห็น

ค้นหาในเว็บไซต์

เนื้อเพลงที่ได้รับความนิยม ในรอบสัปดาห์

ค้นหาบล็อกนี้