base_url() function not working in codeigniter - php

In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
<script type="text/javascript">
//<![CDATA[
base_url = '<?= base_url();?>';
//]]>
</script>
</head>

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.
application/config/config.php, line 13

If you want to use base_url(), so we need to load url helper.
By using autoload $autoload['helper'] = array('url');
Or by manually load in controller or in view $this->load->helper('url');
Then you can user base_url() anywhere in controller or view.

First of all load URL helper. you can load in "config/autoload.php" file and add following code
$autoload['helper'] = array('url');
or in controller add following code
$this->load->helper('url');
then go to config.php in cofig folder and set
$config['base_url'] = 'http://urlbaseurl.com/';
hope this will help
thanks

Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';

I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php
$autoload['helper'] = array('url');
Than You will be able to ue base_url() like this
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />

Apart from making sure you have set config/autoload.php:
$autoload['helper'] = array('url');
Change application/config/config.php from:
$config['base_url'] = 'http://example.com/';
Become a dynamic base url:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https": "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
And using the host from php to run it on local, below is just an example port.
php -S localhost:2000

If you don't want to use the url helper, you can get the same results by using the following variable:
$this->config->config['base_url']
It will return the base url for you with no extra steps required.

Load url helper in controller
$this->load->helper('url');

Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helper function of framework.
While some of Helpers may be available globally to use just like log_message() which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url() is a function defined in url helper of the Framework.
You can learn more about helper in Codeigniter user guide's helper section.
You can use base_url() function once your current class have access to it, for which you needs to load it first.
$this->load->helper('url')
You can use this line anywhere in the application before using the base_url() function.
If you need to use it frequently, I will suggest adding this function in config/autoload.php in the autoload helpers section.
Also, make sure you have well defined base_url value in your config/config.php file.
This will be the first configuration you will see,
$config['base_url'] = 'http://yourdomain.com/';
You can check quickly by
echo base_url();
Reference: https://codeigniter.com/user_guide/helpers/url_helper.html

Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution =>
here is my solution :
application>config>config.php
$config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url
question explanation:
" >
i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ?
1. $autoload['helper'] = array('url');
2. $this->load->helper('url'); in my controllar then i go to my

I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.
Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php
If you include it in controller class method/__constructor then it look like
public function __construct()
{
$this->load->helper('url');
}
or If you load in autoload file then it would looks like
$autoload['helper'] = array('url');
Finally, add your stylesheet file.
You can link a style sheet by different ways, include it in your inside section
-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />
-> or
<?php
$main = array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
);
echo link_tag($main); ?>
-> or
finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder.
Then add some links in styles.php file looks like below
<?php
$config['style'] = array(
'main' => array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
)
);
?>
call/add this config to your controller class method looks like below
$this->config->load('styles');
$data['style'] = $this->config->config['style'];
Pass this data in your header template looks like below.
$this->load->view('templates/header', $data);
And finally add or link your css file looks like below.
<?php echo link_tag($style['main']); ?>

This All Directly Access Function Will Be Loaded Through Helper Class Only.
Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.
config/autoload.php
$autoload['helper'] = array('url', 'file', 'authorization', 'custom');

Related

Echo base URL() rduplicates url again in codeigniter

When I Echo base URL inside href tag it duplicates again in codeigniter.
In config.php base , I set base url as
$config['base_url'] = 'https://www.mysite.in/';
And autoload Url in autoload.php
<link rel="apple-touch-icon" sizes="180x180" href="<?php echo base_url(); ?>assets/img/fav-icons/apple-touch-icon.png">
When I inspect abpove href it shows as
<img tag is also missing
First : you need to add activate url in autoload.php file
$autoload['helper'] = array('url');
Second : you need to add the url in the config.php file
$config['base_url'] = 'https://www.mysite.in';
Third you need to add the code like this
<?php echo base_url("assets/img/fav-icons/apple-touch-icon.png"); ?>
Sample on my browser

Codeigniter bootstrap load only on index.php page

I'm loading bootstrap this way on my view page :
<link rel="stylesheet" href="<? echo base_url('assets/css/bootstrap.min.css')?>">
autoload: $autoload['helper'] = array('url');
configs: $config['base_url'] = 'http://localhost/helloby/';
Below is the path of the controller:
http://localhost/helloby/index.php/login/home
But when I copy the same code at welcome_message view bootstrap loads normally. I don't know why it's happening.
Try using site_url() instead of base_url()
base_url returns the url without index.php
Check if you are loading the bootstrap css in every view, you cant just load it in one view and expect that all other views know about it.
Common way of doing things like this is to create header and footer files that are shared between all views.
I solved the problem, I was reading an e-book of best practice of Codeigniter and the call of bootstrap was a little bit different.
This way : href="<?=base_url('/assets/css/bootstrap.min.css')?>
and working fine!

Codeigniter how to properly use base_url?

I am having a little trouble with base_url in codeigniter, let's say if I don't set my $config['base_url'] in the config/config.php file, calling base_url will produce [::1], this is not what I want, I want it to return localhost, so I set $config['base_url'] = 'localhost', yup it return localhost/bla/bla
when accessed locally, HOWEVER if I access my project remotely from another computer, it DOES keep returning 'localhost' too while what I want is to return the base_url as in the ip address of the server (i.e 192.168.1.8 etc).
I am very sure CI team has thought this through and has a special solution or parameter in the base_url however I wasn't able to find it or I am not searching enough. Anyway thanks in advance.
You can assign base_url dynamically using host
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
To use base_url(), 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().
and use like this
<link rel="stylesheet" href="<?php echo base_url(); ?>css/default.css" type="text/css" />
you can follow this url

CodeIgniter can't read other files in view folder

I'm trying to use an <a> tag to load a file in my view folder but it just won't work.
Records
I already put $this->load->helper('url'); in the function __construct() in my default controller($route['default_controller'] = "residents";)
and set $autoload['helper'] = array('url'); in my autoload.php
I can load all of my view using $this->load->view(); but cant load it using site_url()
The only files that I can load using site_url() are the files in the root directory of codeigniter.
Can anybody tell me what should I do in order to make site_url() load my view files? Thanks
your question is not clear but its a guess
use base_url() instead of site_url().Hope it will work.

How can I include the CSS file in CodeIgniter?

I have make a assets folder in root directory where application folder exist. now I have assets sibling to application.
Now when I tried to open http://localhost/CodeIgniter/assets/css/bootstrap.min.css in web-page it's not working.
Do someone can help me how I can got it worked.
In a solution I found that writing this line in config file will work but it's not work either by doing this change.
$config['base_url'] = 'http://localhost/CodeIgniter/';
Now please some guideline me how to load CSS file in CI.
solution :-
I have done something wrong in my codeigniter project. I download it again and try these thing again, without anything it's working.
thanks you all of guys. you save my time.
In constant.php:
define('URL','http://localhost/CodeIgniter/');
define('IMG',URL.'assets/img/');
define('CSS',URL.'assets/css/');
define('JS',URL.'assets/js/');
In config.php:
$config['base_url'] = URL;
In view:
<link rel="stylesheet" href="<?php echo(CSS.'bootstrap.min.css'); ?>">
Hope that helps somewhat.
define general css that used.
Open "config.php" within directory CodeIgniter\system\application\config.
$config['css'] = 'mystyles.css';
Create a file named mystyles.css within root application: \CodeIgniter.
$this->load->helper('url');
$data['css'] = $this->config->item('css');
You can load css in views pages like this.
Add below line in your controller action /application/controllers/yourController.php
$this->load->helper('url');
Then add below line to your view file's head tag.
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/yourcssfile.css');?>" />
Assuming that you have assets/css/ folder is created in your app directory.
<path to your app folder>/assets/css

Categories