phpxamppcodeigniter-4

CodeIgniter 4 Error stating SetScheme requires string but null given. Even though already passing a string


Im setting up a website using a previous project in Xampp, CodeIgnitor 4, and using PHP and MYSQL for the languages. I recently finished uploading the entire database and updated the location of website to the localhost website.

header('Location: localhost/sig/public/'); public $baseURL = 'localhost/sig/public/';

But when I start Apache and MySql in Xampp and call the link the following error is displayed:

Fatal error: Uncaught TypeError: CodeIgniter\HTTP\URI::setScheme(): Argument #1 ($str) must be of type string, null given, called in C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php on line 445 and defined in C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\URI.php:749 Stack trace: #0 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php(445): CodeIgniter\HTTP\URI->setScheme(NULL) #1 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php(207): CodeIgniter\HTTP\IncomingRequest->setPath('/', Object(Config\App)) #2 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\IncomingRequest.php(169): CodeIgniter\HTTP\IncomingRequest->detectURI('REQUEST_URI', 'localhost/sig/p...') #3 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\Services.php(589): CodeIgniter\HTTP\IncomingRequest->__construct(Object(Config\App), Object(CodeIgniter\HTTP\URI), '', Object(CodeIgniter\HTTP\UserAgent)) #4 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\BaseService.php(192): CodeIgniter\Config\Services::request(Object(Config\App), false) #5 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\Services.php(580): CodeIgniter\Config\BaseService::getSharedInstance('request', NULL) #6 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\Services.php(279): CodeIgniter\Config\Services::request() #7 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\BaseService.php(192): CodeIgniter\Config\Services::exceptions(Object(Config\Exceptions), NULL, NULL, false) #8 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\Config\Services.php(275): CodeIgniter\Config\BaseService::getSharedInstance('exceptions', NULL, NULL, NULL) #9 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\CodeIgniter.php(175): CodeIgniter\Config\Services::exceptions() #10 C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\bootstrap.php(157): CodeIgniter\CodeIgniter->initialize() #11 C:\xampp\htdocs\sig\public\index.php(37): require('C:\xampp\htdocs...') #12 {main} thrown in C:\xampp\htdocs\sig\vendor\codeigniter4\framework\system\HTTP\URI.php on line 749

This is the code that appears in the IncomingRequest.php file:

if ($baseURL !== '' || $baseURL !== null)
{
    echo "<script type='text/javascript'>alert('$baseURL');</script>";
        
    $this->uri->setScheme(parse_url($baseURL, PHP_URL_SCHEME));
    $this->uri->setHost(parse_url($baseURL, PHP_URL_HOST));
    $this->uri->setPort(parse_url($baseURL, PHP_URL_PORT));

    // Ensure we have any query vars
    $this->uri->setQuery($_SERVER['QUERY_STRING'] ?? '');

    // Check if the baseURL scheme needs to be coerced into its secure version
    if ($config->forceGlobalSecureRequests && $this->uri->getScheme() === 'http')
    {
        $this->uri->setScheme('https');
    }
}

Ive placed an echo to see what appears for $baseURL and the previously mentioned "localhost/sig/public/" appears in the alert message. Ive tried making it into a full link with "http://" but as it already sets up the https scheme it throws an error stating that I cant modify the header information that it has already been sent. I have restarted Apache and MySQL in Xampp whenever I updated values in the script. Other posts I have seen related to this error seem to have the solution of restarting Xampp but I have already been doing this each time I change the code base.


Solution

  • The issue is related to the baseurl to localhost\sig\public, which is not a valid URL scheme. in codeIgniter4, the baseURL should be set to a full URL scheme.

    public $baseURL = 'http://localhost/sig/public';
    

    This will tell us the CodeIgniter's current domain and protocol.

    Or you can leave the baseurl blank like this.

    public $baseURL = '/';
    

    Now restart the Apache and see if it works, if not then check that the CodeIgniter config is correct.