Fatal error: Call to undefined function base_url() in C:\wamp\www\Test-CI\application\views\layout.php on line 5
Asked Answered
R

14

49

Hello I am new to CodeIgniter and PHP, I am trying to setup it for the firs time, but it give the following error.

Fatal error: Call to undefined function base_url() in

  1. C:\wamp\www\Test-CI\application\views\layout.php on line 5
     

  2. {main}( ) IN ..\index.php:0 require_once('C:\wamp\www\Test-CI\system\core\CodeIgniter.php' ) IN ..\index.php:202

  3. call_user_func_array ( ) IN ..\CodeIgniter.php:359

  4. Home->index( ) IN ..\CodeIgniter.php:0

  5. CI_Loader->view( ) IN ..\home.php:17

  6. CI_Loader->_ci_load( ) IN ..\Loader.php:419

  7. include('C:\wamp\www\Test-CI\application\views\layout.php' ) IN ..\Loader.php:833

My code :

 <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>Galleriffic | Custom layout with external controls</title>
        <link rel="stylesheet" href="<?php base_url(); ?>/assets/css/basic.css" type="text/css" />
        <link rel="stylesheet" href="<?php base_url(); ?>/assets/css/galleriffic-5.css" type="text/css" />
        
        <!-- <link rel="stylesheet" href="<?php base_url(); ?>/assets/css/white.css" type="text/css" /> -->
        <link rel="stylesheet" href="<?php base_url(); ?>/assets/css/black.css" type="text/css" />
        
        <script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery-1.3.2.js"></script>
        <script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.history.js"></script>
        <script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.galleriffic.js"></script>
        <script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.opacityrollover.js"></script>
        <!-- We only want the thunbnails to display when javascript is disabled -->
        <script type="text/javascript">
            document.write('<style>.noscript { display: none; }</style>');
        </script>
    </head>
Rozina answered 20/7, 2012 at 14:45 Comment(2)
You need URL helper .. refer codeigniter.com/user_guideCloistered
possible duplicate of base_url() function not working in codeigniterFrivol
L
176

You have to load the url helper to access that function. Either you add

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

somewhere in your controller.

Alternately, to have it be loaded automatically everywhere, make sure the line in application/config/autoload.php that looks like

$autoload['helper'] = array('url');

has 'url' in that array (as shown above).

Langland answered 20/7, 2012 at 14:51 Comment(1)
this works perfectly, was having issues generating link with base_url for my anchor tagFunctional
S
20

just add

$autoload['helper'] = array('url');

in autoload.php in your config file

Skean answered 17/10, 2014 at 5:26 Comment(1)
I personally would discourage using autoload for new users ... it is better to do it manually over and over again (as with everything) to learn it solidly AND THEN you can start using shortcuts ... just my opinion ...Orogeny
R
12

You need to load the helper before loading the view somewhere in your controller.

But I think here you want to use the function site_url()

Before you load your view, basically anywhere inside the method in your controller, add this to your code :

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

Then use the function site_url().

Real answered 20/7, 2012 at 14:51 Comment(0)
V
6

There is only three way to solve. paste in following code in this path application/config/autoload.php.

most of them I use this

require_once BASEPATH . '/helpers/url_helper.php';

this is good but some time, it fail

$autoload['helper'] = array('url');

I never use it but I know it work

$this->load->helper('url');
Vltava answered 7/8, 2018 at 19:28 Comment(0)
U
5

To load other file(css ,js) and folder(images) , in codeigniter project

we have to follow two simple step:

1.create folder public (can give any name) in your codeigniters folder put your bootstrap files css and js folder in side public folder. https://i.sstatic.net/O2gr6.jpg

2.now we have to use two line of code 1.load->helper('url'); ?> 2.

go to your view file

put first line to load base URL

   <?php $this->load->helper('url'); ?>

secondly in your html head tag put this

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

now you can enjoy the bootstrap in your codeigniter

Ultramicrochemistry answered 20/9, 2017 at 7:4 Comment(0)
V
5

Go to application/config/autoload.php

$autoload['helper'] = array('url'); 

add this on top anywhere

and at this in controller

function __construct()
{
    parent::__construct();
$this->load->helper('url');

}
Vltava answered 24/9, 2020 at 20:8 Comment(0)
A
4

you have to use echo before base_url() function. otherwise it woudn't print the base url.

Alarmist answered 19/12, 2012 at 7:47 Comment(0)
G
3

codeigniter needs to load the url helper to call the base_url() function.

use application->config->autoload.php

edit autoload helper array and add url helper..

base_url() function is working now..

Gearldinegearshift answered 12/1, 2016 at 6:52 Comment(0)
L
1

first you have to give echo to display base url. Then change below value in your autoload.php which will be inside your application/config/ folder. $autoload['helper'] = array('url'); then your issue will be resolved.

Lui answered 6/12, 2013 at 9:35 Comment(0)
R
0

Check that you are extending CI_Controller class in your controller and in the view you have to echo base_url($path) function, otherwise it will not work.

Runin answered 20/7, 2012 at 14:51 Comment(0)
C
0

Simply add $autoload['helper'] = array('url'); to autoload.php.

Carson answered 21/5, 2015 at 10:18 Comment(3)
@kenorb Yes it does provide an answer. It's even the correct one. Unfortunately the question is also a duplicate.Frivol
@holodoc It could be, but the answer was copied from other up-voted answers, so it should be removed anyway. There are already too many the exact same answers which was posted later on. So either people doesn't read other answers, or they think they could gain some points.Ophiolatry
@Ophiolatry Copied or not its still a valid answer. Voting habits of the community are a completely different topic. Either way, question is flagged as duplicate now, will probably get closed.Frivol
H
0

Just create a variable as $base_url

$base_url = load_class('Config')->config['base_url'];

<?php echo $base_url ?>

and call it in your code..

Hypopituitarism answered 25/2, 2017 at 6:47 Comment(0)
R
0

..

here i set value of href is the path of file you get this path by following step

search localhost on browser then go in directory where file saved & then copy url

note* ** if you have any index.php or index.html or other index named file change the name temporary for get url then after you undo file name **

use this not need use function

Rhumb answered 21/7, 2022 at 14:34 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Alephnull
R
0

<link href="http://localhost/CodeIgniter-2.2.6/css/sm_reg.css" rel="stylesheet">

here i set value of href is the path of file you get this path by following step

search localhost on browser then go in directory where file saved & then copy url

note* ** if you have any index.php or index.html or other index named file change the name temporary for get url then after you undo file name **

use this not need use function

Rhumb answered 21/7, 2022 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.