CodeIgniter Forums
Tutorial code not working for site_url and some inconsistencies in site_url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Tutorial code not working for site_url and some inconsistencies in site_url (/showthread.php?tid=67489)



Tutorial code not working for site_url and some inconsistencies in site_url - bhagwandas - 02-28-2017

I started CodeIgniter recently and found few inconsistencies which I could not understand:

In this page:
https://www.codeigniter.com/user_guide/tutorial/news_section.html#

It says to use code:

$this->load->helper('url_helper');

But User guide for same at :
https://www.codeigniter.com/user_guide/helpers/url_helper.html

Says to use following code:
$this->load->helper('url');

Why two different versions?

Now comes my real problem:
$config['base_url'] = 'localhost/ci_host/'; // This was also not mentioned in tutorial.

My relevant code for application/views/news/index.php 
is following:

<?php echo "Base url: ". base_url('news');?>
<?php echo "Site url: ". site_url('news');?>
<?php echo "Slug: ". $news_item['slug'];?>
<p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

And out put for same is: 
Base url: localhost/ci_test/news
Site url: localhost/ci_test/index.php/news
Slug: new_item_1
Base url for slug: localhost/ci_test/news/new_item_1


View article

And "View article" takes me to following link:
http://localhost/ci_test/index.php/localhost/ci_host/index.php/news/new_item_1

But inspect on "View Article" shows following:

<a href="localhost/ci_host/index.php/news/new_item_1">View article</a>

Would be great to take me out of this.

Totally baffling me ...
My codeigniter base is located at: http://localhost/ci_test/


RE: Tutorial code not working for site_url and some inconsistencies in site_url - kenjis - 03-01-2017

How about this?
PHP Code:
$config['base_url'] = 'http://localhost/ci_host/'



RE: Tutorial code not working for site_url and some inconsistencies in site_url - bhagwandas - 03-01-2017

Wow..that works. Thanks a lot @kenjis .

Putting http:// really helped.

By any chance, could you enlighten me on why this happened? I am ready to get deep into the code.

But frankly these tidbits is what, pushes me away from using Frameworks. I am hoping these are less in CodeIgniter as it is very lightweight.


RE: Tutorial code not working for site_url and some inconsistencies in site_url - InsiteFX - 03-01-2017

When loading a helper you drop the _helper off of it and just use the name url_helper becomes url


RE: Tutorial code not working for site_url and some inconsistencies in site_url - kenjis - 03-01-2017

First, I recommend you read the comment for $config['base_url'].
PHP Code:
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| WARNING: You MUST set this value! 
`localhost/ci_host/` is not correct URL.


Quote:And "View article" takes me to following link:
http://localhost/ci_test/index.php/localhost/ci_host/index.php/news/new_item_1

But inspect on "View Article" shows following:
<a href="localhost/ci_host/index.php/news/new_item_1">View article</a>
Perhaps you are at `http://localhost/ci_test/index.php/` and the href is `localhost/ci_host/index.php/news/new_item_1`.
So you will go `http://localhost/ci_test/index.php/localhost/ci_host/index.php/news/new_item_1`. Right?


RE: Tutorial code not working for site_url and some inconsistencies in site_url - kenjis - 03-01-2017

(03-01-2017, 12:21 AM)bhagwandas Wrote: But frankly these tidbits is what, pushes me away from using Frameworks. I am hoping these are less in CodeIgniter as it is very lightweight.

Yes, conventions in CodeIgniter are less than any other frameworks.
But it still has some.


RE: Tutorial code not working for site_url and some inconsistencies in site_url - bhagwandas - 03-03-2017

Thanks a lot @kenjis .

Reinforced my faith in CodeIgniter.

All my doubts are cleared now. I was taking it wrong because of my poor understanding with html.