codeigniter include common file in view
Asked Answered
S

9

20

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?

Sekyere answered 8/5, 2013 at 17:0 Comment(1)
There are so many answer given below but it depends on the size of your common file whether you want to make a helper function or directly view.but according to me you should always keep js and php in different files for good practices !!Bently
D
39

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');
}
Domingadomingo answered 8/5, 2013 at 17:4 Comment(5)
read what MVC stands for. This is how CodeIgniter is build. If all of us would build everything where we want, without knowing what we do, what's the point in using MVC standards or a framework?Nuthatch
This is bad practices you cannot load same view for all the request all the time you should use helpersBently
@saurabh2836: There is no need to pull it in for every request all the time. If you see my examples, you only include the view when you need it (whether that be through the view or through the controller).Domingadomingo
@xbonez as per the question common view will be required through out the project so you have to load this file in each function i think am i correct!!!!Bently
Not in each function, just the ones you need it in. If this is done as a helper, wouldn't you need to call the helper multiple times as well?Domingadomingo
K
2

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

Koehn answered 8/5, 2013 at 17:4 Comment(0)
G
2

How To Make Global Functions In CodeIgnitor

To create global functions, define them in CodeIgnitor Helper files and auto load them. Here's how:

Create Your Helper

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 */

    }
}

Making Them Global

If you use these functions all the time (Global), auto-load them.

  1. Open the file: /config/autoload.php
  2. Find the section $autoload['helper'] = array();
  3. Add the name of your helper file (excluding the.php extension).

For instance, if you created a helper file called myhelper.php, It should look something like this:

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

Using Your Global Functions

Now your functions will be available throughout the site. Just call them wholesale:

my_sample_function('argument');

Godgiven answered 20/7, 2014 at 21:6 Comment(0)
N
1
  1. 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

  2. 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/

  3. 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.

Nuthatch answered 8/5, 2013 at 21:40 Comment(6)
Ok but if i want to load this function in certain pages and not in every? because for example I have a jQuery function and don't want that this function works in other pagesSekyere
Is this library of yours a php helper to build javascript functions (like this)? Or is it a simple javascript function?Nuthatch
This file contain jQuery function with some PHP code inside, isn't an helper, only pure functionSekyere
Usually, the javascript functions will go between <head></head> tags and in document ready, like thisNuthatch
I know, but I have a lot of common function and isn't correct to repeat in every pageSekyere
I structure my website like this: have a header, content and then footer. On my header there are the javascript functions (you can also put them in a .js file). Then I call the header on every page that I build. Hope this clears your mind.Nuthatch
S
1

You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');

Shanda answered 29/7, 2014 at 12:29 Comment(0)
P
1

You Can include anyware using

<?php $this->load->view('file'); ?>
Polyandrist answered 13/2, 2018 at 9:49 Comment(0)
H
0

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

}
Hartford answered 27/10, 2015 at 14:59 Comment(0)
H
0

Suppose I want to include a sidebar then I will write the code like this

<?php include 'sidebar.php';?>
Heel answered 5/6 at 4:23 Comment(0)
M
-2

You can write php function in helper file steps - create a helper file name common_helper inside application/helperfolder - 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

Migrant answered 5/10, 2015 at 6:29 Comment(1)
You do not type .php when calling load->view().Quarters

© 2022 - 2024 — McMap. All rights reserved.