Search code examples
phpcodeigniterroutescodeigniter-4

Some Route Resource Not Working in Codeigniter 4


I cannot find the problem using

$routes->resource

Please help me figure out what is the problem.

This is how I put my routes resource in config routes :

$routes->resource('ApiManageBanner', ['controller' =>'App\Controllers\ApiData\ApiManageBanner']); // get, put, create, delete

Recently I just move all my project to the newest codeigniter 4 version 4.2.6 from the previous version 4.1.2

This is my controllers :

<?php
 
 namespace App\Controllers\ApiData;

 use App\Controllers\BaseController;
 use CodeIgniter\RESTful\ResourceController;
 use Codeigniter\API\ResponseTrait;
 


class ApiManageBanner extends ResourceController
{    
    use ResponseTrait;
    function __construct()
    {       

    }
    

    // equal to get    
    public function index() {    

        echo "Test";       
        
    }
    
        
    // equal to post
    public function create() {
            

    }

    // equal to get
    public function show($id = null) {                
      
    }

    // equal to put    
    public function update($id = null) {
        
    }
    

    // equal to delete
    public function delete($id = null) {        
       
        
    }

}

I just try a simple to echo "Test".

But I got this error :

enter image description here

I search everywhere but cannot find the problem related to the error.

If I change the routes name to 'ApiManageBanners' using 's' :

$routes->resource('ApiManageBanners', ['controller' =>'App\Controllers\ApiData\ApiManageBanner']); // get, put, create, delete

It is working.

But I cannot change my routes name because my application is reading 'ApiManageBanner' not 'ApiManageBanners'

I am very curious what cause the problem. It is not working for almost all my resources api controller routes.


Solution

  • I found the problem. According to the error it is related to session. When I check all my file. I found that I always init the :

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

    In all my controller and model __construct();

    function __construct()
        {
                                    
            $this->Session = \Config\Services::session();
                    
        }
    

    So I remove it and init globaly in BaseController.php

    <?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 =   [''];
    
        /**
         * Constructor.
         */
        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.
    
            // E.g.: $this->session = \Config\Services::session();
            $this->session = \Config\Services::session();       
            $this->language = \Config\Services::language();
            $this->language->setLocale($this->session->lang);
        }
    }
    

    Then the error is gone.