简体   繁体   中英

Codeigniter URL and base_url() confusion

 $config['base_url'] = 'localhost/project';

 <link href="<?= base_url(); ?>/css/bootstrap.min.css" rel="stylesheet">

Would produce an error and it won't load my css on my view because of double '/'.

This is the pagesource of the view on a browser.

<link href="localhost/project//css/bootstrap.min.css" rel="stylesheet">

And the css won't load. When i try the page source and follow the link, it leads to 404 error.

What i'm confused about is that when i add protocol on my base_url on config file.

<link href="http://localhost/project//css/bootstrap.min.css" rel="stylesheet">

For reasons unknown, the above url seems to point to the css and it loads on my view even though it still has double '/' on the url between 'project' and 'css'. Also, even if i remove the '/' before the 'css' on the view, it will load, so long as i add a protocol on my base url even though without the protocol even with no double '/' it still won't load the css.

<link href="http://localhost/project/css/bootstrap.min.css" rel="stylesheet">

I also have some questions,

On my config base url above, i never put an '/' at the end so why does it add an '/' when i use the function base_url on my view. Why is it like this?

My 2nd question, is that the '/' on my url.

<link href="css/bootstrap.min.css" rel="stylesheet">

<link href="/css/shop-homepage.css" rel="stylesheet">

The first link when clicking on the url on the view pagesource on my browser, it still has the controller in the url.

http://localhost/project/Pages/css/bootstrap.min.css

While if i add a '/' before the 'css' it removes the 'project' and 'Pages' in the url.

http://localhost/css/shop-homepage.css

I could solve the problem just that i don't know why they occur so even though i can get my css to load i don't feel right because i don't know why.

The base_url is part of the URL Helper .

The problem is in our config file, the configuration file says that:

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = 'http://localhost/site/';

You should have added the slash, and since you didn't, the base_url() function adds it automatically.

The best usage is to provide the file location as a parameter, as of

<link href="<?= base_url("css/bootstrap.min.css"); ?>" rel="stylesheet">

Ideally your base_url in the config should be like:

$config['base_url'] = "http://www.example.com/";

and in your view you can add folder name and file name that you need to include.

<link href="<?= base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

This way there will be no confusions or double '/'

Plus prefer using this:

<link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

You simply can't start a URL with the hostname:

localhost/project

Otherwise, it'd be impossible to tell out whether you mean a domain name or a location within your server.

You need a full protocol prefix:

http://localhost/project

... or, if already in the context of HTTP, at least the double slash:

//localhost/project

Browsers often prefer to hide the actual URL (thus the widespread confusion) but the underlying value works exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM