Welcome Guest, Not a member yet? Register   Sign In
Proper setup of baseURL and usage of route_to
#1

Hello.

It's my first post here, first time I am using Code Igniter, first time in 10 years I am writing anything in PHP (at the time, those were only plugins for MyBB forum engine). So yes, I might lack "a bit" in the field of experience with web-dev, so please forgive me if what I will ask about here will be "too obvious".

I have problem with relative links generated by route_to. But let me start with config info.

For now I am running the project locally with WAMP.
Project is located at localhost/test, so I've setup config accordingly:
PHP Code:
public $baseURL 'http://localhost/test/';   // tried it also without trailing slash 


I've also set up base element in header:
Code:
<base href="http://localhost/test/">
(here started without this tag, also when added, tried with and without trailing slash)

Now for my Routes.php contents:
PHP Code:
$routes->get(    '/',                'Home::index',                [ 'as' => 'home' ] );
$routes->get(    '/index.php',       'Home::index' );
$routes->get(    '/index.html',      'Home::index' );

$routes->get(    'login',            'Login::showLoginPage',       [ 'as' => 'login_show' ] ); 
(as you can see, again I played with slashes)

When I use route_to function in templates/views like this:
Code:
<a href="<?= route_to( 'home' ); ?>">LINK</a><br>
<a href="<?= route_to( 'login_show' ); ?>">LINK</a><br>
I am receiving these links:
Code:
localhost            // instead of localthost/test
localhost/login      // instead of localthost/test/login

After trying whatever I could think about, with trailing slash in the HTML base tag and dots added to every href, links started working properly. Tutorial does not mention the requirrement of using dots in hrefs, so am I missing something? Have I missed something in config that I have to use that kind of workaround? I am a bit worried that when project grows a bit more (e.g. when I will need something like localhost/test/part1/par2/part3), I will face another URI problem, that I won't be able to solve so easily.
Reply
#2

(This post was last modified: 05-07-2020, 10:58 AM by jreklund.)

Hi, welcome to the forum.

What that function outputs are relative urls, not absolute url like site_url and base_url.
Code:
<a href="/">LINK</a><br>
<a href="/login">LINK</a><br>

It's not appending $baseURL. If you want absolute urls you can do:
Code:
<a href="<?= base_url(route_to( 'home' )); ?>">LINK</a><br>
<a href="<?= base_url(route_to( 'login_show' )); ?>">LINK</a><br>

Or fail safe with site_url, so that it appends index.php
Code:
<a href="<?= site_url(route_to( 'home' )); ?>">LINK</a><br>
<a href="<?= site_url(route_to( 'login_show' )); ?>">LINK</a><br>
Reply
#3

Thanks. I might use one of these.

And no, now I know that route_to does not return relative links. I had to read a bit more. If link starts with a slash, it always goes back to the root, not to the base address. So in browser obviously all is working as expected, except route_to does not return relative url (user guide claims it does, but also follows that claim with link that starts with a slash). I did not read docs (if something else than user guide exists), but I would consider this being a bug.
Reply
#4

(05-06-2020, 10:58 AM)jreklund Wrote: Hi, welcome to the forum.

What that function outputs are relative urls, not absolute url like site_url and base_url.
Code:
<a href="/">LINK</a><br>
<a href="/login">LINK</a><br>

It's not appending $baseURL. If you want absolute urls you can do:
Code:
<a href="<?= base_url(route_to( 'home' )); ?>">LINK</a><br>
<a href="<?= base_url(route_to( 'login_show' )); ?>">LINK</a><br>

Or fail safe with site_url, so that it appends index.php
Code:
<a href="<?= site_url(route_to( 'home' )); ?>">LINK</a><br>
<a href="<?= site_url(route_to( 'login_show' )); ?>">LINK</a><br>

===
Thanks 
It's helping my problem
Reply




Theme © iAndrew 2016 - Forum software by © MyBB