Welcome Guest, Not a member yet? Register   Sign In
Problem with CI URL's in template
#1

[eluser]JohnDoerther[/eluser]
Hey,

First of all thanks a bunch for the great framework CI is, I just got started with CI and really love it!

Currently, I am using a template view which looks like this

$this->load->view('back/includes/header');
$this->load->view($main_content);
$this->load->view('back/includes/footer');

The header file contains a menu with 3 links to views: back/home, back/users and back/products.

When I navigate to different views (for example there is a view users/details/1), the links in the header files stay the same (for example, <a href="back/home">home</a> for the home screen. But when I am at a page that is at another level in the hierarchy, these static links do not work anymore.

For example when I am at back/users/details/1, the static link leads to back/users/details/back/home which should in fact be just back/home.

I have been looking into base_url() but this is set to "http://127.0.0.1/system/application/" in my config file, and when I use the base_url to create the links this does not really seem to work...

Is there an easy way to fix this, in other words what is best practice to make static links to CI pages, that will work from different levels in the hierarchy? Thanks!
#2

[eluser]pickupman[/eluser]
First, welcome to the boards and CI. You will find it best to use
Code:
<a href="&lt;?php site_url('back/users/details/1');?&gt;">Users</a>
//or
&lt;?php echo anchor('back/users/details/1','Users');?&gt;

You will need to (auto)load the url helper. Either method works best, as you can change index.php, and these functions will generate the correct full url. If you are currently creating links manually (standard html), relative links are based on the current url. So links like
Code:
<a href="/back/users">Users</a>
This will always be relative to the document root. http://site.tld

Code:
<a href="back/users">Users</a>
This will create links relative to the current folder in url. So if page was http://site.tld = http://site.tld/back/users. But if url is http://site.tld/back/users = http://site.tld/back/users/back/users
#3

[eluser]JohnDoerther[/eluser]
Hey pickupman,

Thanks for the welcome, and thanks for the very quick response - this is really appreciated, thanks a bunch.

I had a look at site_url and this works like a charm!

A last question if you have some more time (I understand if you are busy, so no worries) - to include CSS in my HTML pages I now use this:

Code:
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url();?&gt;system/application/style/back/master.css" /&gt;

This works, but is it good practice? Should I ever have to use system/application in my URLs? I am not sure where to place my CSS files, I placed them in the application folder but not sure if this is the best way to do this.

Again, thanks for the quick feedback pickupman, have a nice day.
#4

[eluser]pickupman[/eluser]
Yes, that will work, although there is a slight performance lost. Any file that has http:// has to be resolved by the browser, and then retrieve the file. When you omit http:// then the browser uses the resource that has already resolved the address.
There are some people have posted asset_url() helper functions to do this automatically. Or you can just simply use
Code:
&lt;link rel="stylesheet" type="text/css" href="/system/application/style/back/master.css" /&gt;




Theme © iAndrew 2016 - Forum software by © MyBB