致命错误:在第 5 行调用 C:\wamp\www\Test-CI\application\views\layout.php 中未定义的函数 base_url()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11581636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:48:47  来源:igfitidea点击:

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

phpcodeigniter

提问by user1541058

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

您好,我是 CodeIgniter 和 PHP 的新手,我正在尝试第一次设置它,但它给出了以下错误。

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

致命错误:调用未定义的函数 base_url()

  1. C:\wamp\www\Test-CI\application\views\layout.php 第 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>

回答by complex857

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

您必须加载urlhelper 才能访问该功能。要么你添加

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

somewhere in your controller.

在你的控制器的某个地方。

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

或者,要让它在任何地方自动加载,请确保application/config/autoload.php中的行看起来像

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

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

'url'在那个数组中(如上所示)。

回答by Hareesh

just add

只需添加

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

in autoload.php in your config file

在您的配置文件中的 autoload.php 中

回答by David Bélanger

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()

但我想在这里你想使用这个功能 site_url()

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

在加载视图之前,基本上在控制器方法内的任何位置,将其添加到您的代码中:

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

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

Then use the function site_url().

然后使用函数 site_url()

回答by Yogesh Wankhede

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

在codeigniter项目中加载其他文件(css,js)和文件夹(images)

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.stack.imgur.com/O2gr6.jpg

1.在您的codeigniters文件夹中创建文件夹public(可以给任何名称)将您的引导文件css和js文件夹放在公共文件夹中。https://i.stack.imgur.com/O2gr6.jpg

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

2.现在我们必须使用两行代码 1.load->helper('url'); ?> 2。

go to your view file

转到您的视图文件

put first line to load base URL

把第一行加载基本 URL

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

secondly in your html head tag put this

其次在你的 html head 标签中放这个

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

now you can enjoy the bootstrap in your codeigniter

现在您可以在 codeigniter 中享受引导程序

回答by Syed Ali

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

只有三种方法可以解决。将以下代码粘贴到此路径 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');

回答by newday

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

你必须使用echobeforebase_url()函数。否则它不会打印基本网址。

回答by Rakesh Gupta

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

codeigniter 需要加载 url helper 来调用base_url()函数。

use application->config->autoload.php

application->config->autoload.php

edit autoload helper array and add url helper..

编辑自动加载助手数组并添加 url 助手..

base_url()function is working now..

base_url()功能现在正在运行..

回答by Rakesh Gupta

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.

首先,您必须给 echo 以显示基本网址。然后在您的 autoload.php 中更改以下值,该值将位于您的 application/config/ 文件夹中。 $autoload['helper'] = array('url');那么您的问题将得到解决。

回答by kjurkovic

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

检查您是否CI_Controller在控制器中扩展类,并且在您必须echo base_url($path)运行的视图中,否则它将无法工作。

回答by Vinit Vijayan

Just create a variable as $base_url

只需创建一个变量 $base_url

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

<?php echo $base_url ?>

and call it in your code..

并在您的代码中调用它..