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

การตั้งค่า 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 . ...

จะลบ index.php ออกจาก URL ใน CodeIgniter 4 ได้อย่างไร

CodeIgniter คืออะไร? CodeIgniter เป็น  เฟรมเวิร์กที่ขับเคลื่อนด้วย PHP  สำหรับการพัฒนาแอปพลิเคชันอย่างรวดเร็ว ในขณะที่สร้างเว็บแอปพลิเคชัน เราใช้เวลาส่วนใหญ่ในการเขียนโค้ดเดิมซ้ำแล้วซ้ำอีก เฟรมเวิร์กเป็นบล็อกเริ่มต้นและลดจำนวนโค้ดที่จำเป็นในการสร้างเว็บไซต์ให้เหลือน้อยที่สุด CodeIgniter เป็นเฟรมเวิร์กแอปพลิเคชันเว็บ  PHP เชิงวัตถุ  ที่เป็นโอเพ่นซอร์สและใช้งานง่ายฟรี CodeIgniter ใช้ MVC โดยมีไลบรารีสำหรับเชื่อมต่อกับฐานข้อมูลและดำเนินการต่างๆ เช่น การส่งอีเมล การอัปโหลดไฟล์ ฯลฯ สำหรับการใช้ Codeigniter การมี  ความรู้พื้นฐานเกี่ยวกับไวยากรณ์ PHP และวิธีการโต้ตอบกับฐานข้อมูลและ HTML ถือเป็นเรื่องดี ทำไมต้องลบ index.php? การลบ index.php จะทำให้ URL ดูสะอาดตาและเป็นมืออาชีพ กรอบงาน Codeigniter ให้บริการมุมมองทั้งหมดผ่านไฟล์เดียว index.php หากไม่มีไฟล์นี้ ไม่มีโมเดล/มุมมอง/คอนโทรลเลอร์จะไม่ทำงาน เราจำเป็นต้องลบ index.php ออกจาก URL ด้วยการกำหนดค่าที่เหมาะสม ไม่เช่นนั้นมันจะแสดงหน้า 404 การลบไฟล์ index.php เปิดไฟล์ \App\Configs\App.php ในโปรแกรมแก้ไขข้อความและลบ index....