• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone check base_url() and base_url("param")

#1
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
Reply

#2
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?
Reply

#3
@SteveDroz,

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;
   
    $current 
=($key === $uri->getSegment(1'home') )
          ' current '  
          
''
          ;

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


The above script $URL is correct and displays:

Quote:$URL = base_url() .'/' .$key;
==>
http://localhost/ci4-strict.tk/public_html/home
http://localhost/ci4-strict.tk/public_html/bwci4
http://localhost/ci4-strict.tk/public_html/playground
http://localhost/ci4-strict.tk/public_html/info
http://localhost/ci4-strict.tk/public_html/lhost

Using this script in accordance with the manual produces:
Quote:$URL = base_url($key);
==>
http://localhost/ci4-strict.tk/public_html/ci4-strict.tk/public_html/home
http://localhost/ci4-strict.tk/public_html/ci4-strict.tk/public_html/bwci4
http://localhost/ci4-strict.tk/public_html/ci4-strict.tk/public_html/playground
http://localhost/ci4-strict.tk/public_html/ci4-strict.tk/public_html/info
http://localhost/ci4-strict.tk/public_html/ci4-strict.tk/public_html/lhost
Reply

#4
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:
base_url($somethingWithASlash); 


or

PHP Code:
base_url('/'.$somethingWithoutASlash); 
Reply

#5
@SteveDroz,

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:

http://example.com/blog/post/123

I was hoping someone could confirm the script and if the output is the same as mine I will raise a error request.
Reply

#6
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.
Reply

#7
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:
Code:
if (isset($_REQUEST))
{
   unset($_REQUEST);
   return redirect()->to(base_url());
}
or
Code:
if ($honeypot->hasContent($request))
{
   return redirect()->to(base_url('Index/tools'));   // that'll feck 'em!
}
Reply

#8
You do not need the base_url in route_to if you have defined a route for that controller.

Example:

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>
</
li
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.