CodeIgniter Forums

Full Version: Can someone check base_url() and base_url("param")
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been trying to correctly use the base_url() function and having trouble Sad

I find that:

1. echo base_url() issues the correct URL

2. echo base_url(). $param  issues the correct URL

3. echo base_url($param) issues an INCORRECT URL

I have checked the ./system/Helpers/url_helper.php source code and it is not easy when trying to debug using the Sublime text editor Sad
Hi, I'd need 3 more informations to answer that question:

1. Where did you write that code (in which controller/model/view/library/…)?
2. What is the incorrect result you get?
3. What is the expected result?

This is the View script
PHP Code:
<?php declare(strict_types=1);

$aLinksTop = [
  'home'        => 'Home',
  'bwci4'       => 'Built with CI4',
  'playground'  => 'Playground',
  'info'        => 'Info',
  'lhost'       => 'Localhost',

  $sLinksTop ''
  foreach($aLinksTop as $key => $link) :
    $URL base_url() .'/' .$key;
=($key === $uri->getSegment(1'home') )
          ' current '  

    $tmp = <<< ____EOT
    <a class="
$key $current"  href="$URL"> $link </a> 
    $sLinksTop .= $tmp;

The above script $URL is correct and displays:

Quote:$URL = base_url() .'/' .$key;

Using this script in accordance with the manual produces:
Quote:$URL = base_url($key);
Ah, I see!

Your problem comes from the fact that base_url($param) uses relative URIs. That means that it will display a page according to your current location.

To avoid that, you can prefix your path with a "/" :

PHP Code:


PHP Code:

According to the CI4 manual it is not necessary to precede the parameter with a leading slash. Also I think that would give the URL two slashes.

From the manual:
Quote:echo base_url('blog/post/123');
The above example would return something like:

I was hoping someone could confirm the script and if the output is the same as mine I will raise a error request.
Oh yeah… I looked at the code and saw it was using relative URIs. I must have misread a line, sorry about my false answer.

I don't intend to try to correct your way of doing, you seem way more expert than I am with CI4, but I don't understand why you put that code in the View instead of the Controller, so I'm not sure how to help you further.
I make extensive use of the base_url() function in my code... and it still seems to be working fine under 4.0.4 (and some of its later commits).

Though, I typically use it like this:
if (isset($_REQUEST))
   return redirect()->to(base_url());
if ($honeypot->hasContent($request))
   return redirect()->to(base_url('Index/tools'));   // that'll feck 'em!
You do not need the base_url in route_to if you have defined a route for that controller.


PHP Code:
$routes->group('', ['namespace' => 'Insitefx\Admin\Controllers'], function($routes)
    $routes->get('dashboard''Dashboard::index', ['filter' => 'login']);

And to go to my dashboard:

PHP Code:
<li class="nav-item d-none d-sm-inline-block">
a href="<?= route_to('dashboard');?>" class="nav-link">Dashboard</a>