Reputation: 35963
Hi all I have a site developed in codeigniter and I wanto to store into a file called common.php some javascript/PHP function that I use in many pages. I have tried in this mode:
require(base_url().'application/libraries/common.php'); //I have tried also include
This return me this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
I'm going to my php.ini and I turn On allow_url_include, restart apache and when I try to load the page return me now this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
Filename: backend/hotel_view.php
Line Number: 6
A PHP Error was encountered
Severity: Warning
Message: require(http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php) [function.require]: failed to open stream: no suitable wrapper could be found
Filename: backend/hotel_view.php
Line Number: 6
Fatal error: require() [function.require]: Failed opening required 'http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/public/klikkahotel.com/application/views/backend/hotel_view.php on line 6
What can I do to include a simple file into my pages?
Upvotes: 20
Views: 99213
Reputation: 1
Suppose I want to include a sidebar then I will write the code like this
<?php include 'sidebar.php';?>
Upvotes: 0
Reputation: 191
You can write php function in helper file
steps - create a helper file name common_helper
inside application/helper
folder
- and create a function like
function getBoosters($id){
$ci=& get_instance();
$ci->load->database();
$sql = "select * from boosters where id ='".$id."' ";
$query = $ci->db->query($sql);
return $query->result();
}
This common function you can use where you want by loading this helper.
Suppose you want to use this method in FrontController simply load the helper by this line
$this->load->helper('common');
Now you can call the method. Add your js code in footer.php
, all the functions are available on every page you can easily use them
Upvotes: -2
Reputation: 633
For solve in more efficient way this problem I have done so:
You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
In your controller you call only one view in respect of MVC and call the functions from your custom helper.
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
Upvotes: 0
Reputation: 687
You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');
Upvotes: 1
Reputation: 68
To create global functions, define them in CodeIgnitor Helper files and auto load them. Here's how:
To create [helpers][2], create a .php file in the application/helpers/
folder and save your functions there.
Note: It is good practice to use the following format, to avoid function name collisions:
if ( ! function_exists('my_function_name'))
{
function my_function_name($arg)
{
/* Your code here */
}
}
If you use these functions all the time (Global), auto-load them.
/config/autoload.php
$autoload['helper'] = array();
For instance, if you created a helper file called myhelper.php, It should look something like this:
$autoload['helper'] = array('myhelper');
Now your functions will be available throughout the site. Just call them wholesale:
my_sample_function('argument');
Upvotes: 2
Reputation: 3236
base_url()
refers to the web path like http://localhost/myproject/
. You cannot include a remote file, actually you should not. It's a security risk. See Can't include file on remote server
Building a custom library is a good choice and if you are using it a lot in your website, you can include it in application/config/autoload.php
under the section $autoload['libraries']
. It will autoload every time you reload the application/website based on codeigniter. Example: $autoload['libraries'] = array('common');
if your library is called common.php
and is located in application/libraries/
DO NOT put functions into a viewer, that's why libraries and helpers exists. A viewer should contain only what a user should see. Example: a view is some form of visualisation of the model.
Upvotes: 1
Reputation: 42440
Pull it into whichever views you want using $this->load->view('common');
You can include other views from either the controller or the view.
Example 1
your_controller.php
public function index() {
$this->load->view('homepage');
}
views/homepage.php
<?php
$this->load->view('common');
?>
<body>
<!-- html -->
</body>
Example 2
your_controller.php
public function index() {
$this->load->view('common');
$this->load->view('homepage');
}
Upvotes: 39
Reputation: 30565
You should use APPPATH
or BASEPATH
or just type the full path to the file.
For security, require_once
should be passed a local file, not a URL. I wouldn't really suggest using require_once()
in CodeIgniter. It might be better to use:
$this -> load -> view('common_file');
Upvotes: 2