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

การตั้งค่า baseController

 <?php


namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
abstract class BaseController extends Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */   /** ส่วนที่เพิ่มเข้าไป  */
    protected $helpers = ['html','form','url','text','date','cookie','number','array','inflector','filesystem','download','typography','security','language','pager','xml','validation','emoji','max'];
/** ส่วนที่เพิ่มเข้าไป  */
    /**
     * Be sure to declare properties for any property fetch you initialized.
     * The creation of dynamic property is deprecated in PHP 8.2.
     */
    // protected $session;

    /**
     * @return void
     */
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.

        $this->session = \Config\Services::session();
    }
}

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

การลบ public ใน codeigniter 4

Codeigniter 4 จะมี folder public โผล่มา เพื่อให้เราใช้งานในเวอร์ชั่น development ( เวอร์ชั่นที่อยู่ในขั้นตอนการพัฒนา ยังไม่ได้เปิดใช้งานจริง ) และเมื่อนำโปรเจคไปใช้งานจริง เราอาจจะไม่ต้องการให้ชื่อโฟลเด้อ public แสดงบน url วิธี ลบโฟลเด้อ public ออกจาก url สามารถทำได้ง่าย ๆ ใน 3 ขั้นตอน ง่ายๆ ดังนี้ ในโฟลเด้อ public ให้ย้าย ไฟล์ index.php, .htaccess และ robot.txt ออกมาไว้ที่ root ของโปรเจค จากนั้นลบ folder public ทิ้งได้เลย เปิดไฟล์ index.php จากนั้นให้แก้ไขตัวแปร FCPATH ให้พาธถูกต้อง จากเดิม (บรรทัดที่ 34) require FCPATH . ' ../app/Config/Paths.php ' ; แก้ไขเป็น require FCPATH . ' app/Config/Paths.php ' เปิดไฟล์ spark จากนั้นให้แก้ไขตัวแปร pathsPath ให้พาธถูกต้อง (หากเราไม่ทำการเปลี่ยนเราจะใช้ spark ไม่ได้เลย) จากเดิม (บรรทัดที่ 69) define ( 'FCPATH' , __DIR__ . DIRECTORY_SEPARATOR .'public'.DIRCTORY_SEPARATOR ); // Load our paths config file // This is the line that might need to be changed, depending on your folder structure. require FCPATH . ...

การติดตั้ง CodeIgniter 4 แบบ Download

  การติดตั้ง CodeIgniter 4 มีขั้นตอนหลายขั้นตอนที่ควรทำตามดังนี้: ดาวน์โหลด CodeIgniter 4: คุณสามารถดาวน์โหลดได้จาก เว็บไซต์ CodeIgniter โดยเลือกเวอร์ชันที่คุณต้องการ. แตกไฟล์: หลังจากดาวน์โหลดไฟล์ zip ของ CodeIgniter 4 ให้แตกไฟล์ zip นั้น. เปลี่ยนชื่อไดเร็กทอรี: เปิดโฟลเดอร์ที่ได้จากการแตกไฟล์แล้ว และเปลี่ยนชื่อโฟลเดอร์เป็นชื่อโปรเจ็กต์ที่คุณต้องการ. ตั้งค่าฐานข้อมูล: ไปที่ไฟล์ .env ในโฟลเดอร์ของโปรเจ็กต์ของคุณ. ตั้งค่ารายละเอียดของฐานข้อมูล เช่น database.default.hostname , database.default.database , database.default.username , และ database.default.password . สร้างตารางฐานข้อมูล: ใน CLI (Command Line Interface) ของระบบคอมพิวเตอร์, เข้าไปในโฟลเดอร์ของโปรเจ็กต์ของคุณแล้วรันคำสั่ง: php spark migrate สร้าง Controller และ View: ใน CLI, รันคำสั่งเพื่อสร้าง Controller และ View ตัวอย่าง: php spark make :controller MyController php spark make :view my_view ทดสอบ: เปิดเว็บบราวเซอร์และเข้าไปที่ URL ของโปรเจ็กต์ของคุณ เช่น http://localhost/myproject . นอกจากนี้, ยังมีขั้นตอนอื่น ๆ ที่...