Welcome Guest, Not a member yet? Register   Sign In
base_url() not working after upgrade
#1

(This post was last modified: 09-18-2024, 03:48 AM by talu.)

Hi,
I updated CI4 from 4.3.5 to 4.4.8.
Since I have a problem with base_url().

In the controller called by https://site.com, base_url() gives the correct information.
In the controller called by https://site.com/app/logs for example, base_url() falls in error " Unable to parse https://" :
Trace:



  1. SYSTEMPATH\HTTP\SiteURI.php : 120   —  CodeIgniter\HTTP\Exceptions\HTTPException::forUnableToParseURI ()

    Code:
    113        $uri = $this->baseURL . $indexPageRoutePath;
    114
    115        // applyParts
    116        // d($uri);
    117        $parts = parse_url($uri);
    118        // trace();exit;
    119        if ($parts === false) {
    120            throw HTTPException::forUnableToParseURI($uri);
    121        }
    122       
    123        $parts['query']    = $query;
    124        $parts['fragment'] = $fragment;
    125        // d($parts);exit;
    126        // d($this);
    127        $this->applyParts($parts);

  2. SYSTEMPATH\HTTP\SiteURI.php : 396   —  CodeIgniter\HTTP\SiteURI->__construct ()

    Code:
    389        $config            = clone config(App::class);
    390        $config->indexPage = '';
    391
    392        $host = $this->getHost();
    393        // d($relativePath);
    394        // d($config);
    395        // d($host);exit;
    396        $uri = new self($config, $relativePath, $host, $scheme);
    397
    398        // Support protocol-relative links
    399        if ($scheme === '') {
    400            return substr((string) $uri, strlen($uri->getScheme()) + 1);
    401        }
    402       
    403        return (string) $uri;

  3. SYSTEMPATH\Helpers\url_helper.php : 58   —  CodeIgniter\HTTP\SiteURI->baseUrl ()

    Code:
    51      */
    52    function base_url($relativePath = '', ?string $scheme = null): string
    53    {
    54        $currentURI = Services::request()->getUri();
    55        // d($currentURI);exit;
    56        assert($currentURI instanceof SiteURI);
    57
    58        return $currentURI->baseUrl($relativePath, $scheme);
    59    }
    60 }
    61
    62 if (! function_exists('current_url')) {
    63    /**
    64      * Returns the current full URL based on the Config\App settings and IncomingRequest.
    65      *

  4. APPPATH\Controllers\LogsController.php : 17   —   base_url()
  5. SYSTEMPATH\CodeIgniter.php : 943   —  App\Controllers\LogsController->index ()







Any idea ?


Thank you in advance
Reply
#2

(09-18-2024, 03:47 AM)talu Wrote: In the controller called by https://site.com/app/logs for example, base_url() falls in error " Unable to parse https://" :

I don't know why, but "https://" is not a valid URL. So no wonder that CI4 is not able to parse.
Check why the URI is "https://".
Reply
#3

I found the root cause of the problem.
The management of URIs is different between the CI versions.
Reply
#4

Hi @talu , would you be willing to share the details? I seem to be encountering similar issue (https://forum.codeigniter.com/showthread.php?tid=91814) and your solution might just help me out.
Thanks in advance!
Reply
#5

Hi Jan,

My problem came from this code (loginfilter with Myth library) :

PHP Code:
public function before(RequestInterface $request$params null)
 {
 if (! 
function_exists('logged_in'))
 {
 
helper('auth');
 }

 
$current = (string)current_url(true)
 ->
setHost('')
 ->
setScheme('')
 ->
stripQuery('token');

 
$config config(App::class);
 
 
// Make sure this isn't already a login route

 
if (in_array((string)$current, [route_to('login'), route_to('forgot'), route_to('reset-password'), route_to('register'), route_to('activate-account')]))
 {
 return;
 } 


I replaced the code by this :

PHP Code:
public function before(RequestInterface $request$params null)
{
if (! 
function_exists('logged_in'))
{
helper('auth');
}

$current current_url(true);

// Make sure this isn't already a login route

if (in_array($current->getRoutePath(), [route_to('login'), route_to('forgot'), route_to('reset-password'), route_to('register'), route_to('activate-account')]))
{
return;



I hope that's helpful.
Reply
#6

Hi,
I am having almost the same problem. I upgraded from 4.0 to 4.5. I understand that there was a serious change in handling URI in 4.4.
My problem is, I used to extend the URI class, and in Services, in the uri() function, I have:
return new \App\Libraries\EXT_URI($uri);
EXT_URI is a class extending URI.
It used to work. But now CI 4.4 and up says that $uri = service('uri'); // returns the current SiteURI instance.
I have a lot of mess with base_url() and such.
I tried to extend the new class SiteURI instead and return the extended one in service, but it gives a fatal error:
Fatal error: Uncaught TypeError: CodeIgniter\HTTP\SiteURI::__construct(): Argument #1 ($configApp) must be of type Config\App, null given

Sorry if it deviates from the original thread but I think it's related.
I understand that there is a big change in 4.4 but I haven't found any how to update.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB