Viewed   97 times

I have the directory structure like this in code igniter:

 Appsite
    -website
       -application
        -images

When I accessing the image in index.php I used: <img src="http://localhost/Appsite/website/images/images.PNG"

And the href is: <li class=""><a href="http://localhos/tAppsite/website/index.php/home/">Home</a></li>

I think it is not a good practice to include the http://localhost when accessing the images or libraries in code igniter. So I tried to change the $config['base_url'] in config.php to $config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/";

And now I update my image source and other library source I remove the localhost and the name of my directory folder:

<img src="images/images.PNG”> <li class=""><a href= <?php echo base_url;?> /website/index.php/home/">Home</a></li>

But I get errors. it says object not found. Can some help me?

 Answers

5

In Config.php

$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';

# If online site
# $config['base_url'] = 'http://.com/';

In .htaccess (outside application folder) - To remove index.php in URL

RewriteEngine on
RewriteCond $1 !^(index.php|assets|image|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

To accessing URL

<a href="<?php echo base_url();?>contollerName/methodName"> click here</a>

To access image

<img src="<?php echo base_url();?>images/images.PNG”>

To access CSS

<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/style.css"/>

To use base_url load URL helper from autoload.php

Sunday, August 21, 2022
2

Base URL in JavaScript

You can access the current url quite easily in JavaScript with window.location

You have access to the segments of that URL via this locations object. For example:

// This article:
// https://.com/questions/21246818/how-to-get-the-base-url-in-javascript

var base_url = window.location.origin;
// "http://.com"

var host = window.location.host;
// .com

var pathArray = window.location.pathname.split( '/' );
// ["", "questions", "21246818", "how-to-get-the-base-url-in-javascript"]

In Chrome Dev Tools, you can simply enter window.location in your console and it will return all of the available properties.


Further reading is available on this thread

Friday, December 16, 2022
1

To use base_url() (shorthand), you have to load the URL Helper first

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

Or you can autoload it by changing application/config/autoload.php

Or just use

$this->config->base_url();

Same applies to site_url().

Also I can see you are missing echo (though its not your current problem), use the code below to solve the problem

<link rel="stylesheet" href="<?php echo base_url(); ?>css/default.css" type="text/css" />
Saturday, December 10, 2022
 
3

For htaccess I suggest:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?*$ index.php/$1 [L,QSA]

And know that when you kill session CI will create a new 1 on your next Page load (in your case on the redirect) .. Session isn't just for user logins.

Don't forget at app/config/config.php to set

$config["index_page"] = '';
Saturday, September 24, 2022
 
1

I think your problem is that base_url is a function in ci 2+ so try this instead

<link href="<?php echo base_url() ?>css/style.css" 
rel="stylesheet" type="text/css" />

It depends how you defined base_url if you did an ending slash otherwise just add a slash so

/css/style.css
Friday, August 12, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :