Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter include common file in view

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?

like image 774
Alessandro Minoccheri Avatar asked May 08 '13 17:05

Alessandro Minoccheri


People also ask

What is $this in CodeIgniter?

To actually answer your question, $this actually represents the singleton Codeigniter instance (which is actually the controller object). For example when you load libraries/models, you're attaching them to this instance so you can reference them as a property of this instance.

HOW include external php file in CodeIgniter?

Then in the middle of the file edit the two variables $application_folder and $system_path and make sure you're using an absolute path instead of a relative one. Then in your external PHP script just include the external. php file and use the $CI global object to access the whole codeigniter: include '../../external.

What is $data in CodeIgniter?

$data should be an array or an object: http://codeigniter.com/user_guide/general/views.html $data = array( 'title' => 'My Title', 'heading' => 'My Heading', 'message' => 'My Message' ); $this->load->view('results_view', $data); results_view.php <html> <? php //Access them like so echo $title.$


2 Answers

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');
}
like image 51
xbonez Avatar answered Oct 09 '22 14:10

xbonez


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');

like image 2
ajtrichards Avatar answered Oct 09 '22 15:10

ajtrichards