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?
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');
}
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');
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.
Open the file: /config/autoload.php
Find the section $autoload['helper'] = array();
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');
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.
You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');
You Can include anyware using
<?php $this->load->view('file'); ?>
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');
}
}
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
Related
I am trying to include a file for the ExactTarget api inside a controller. Basically, an ajax request submits a form to a route /send. In the controller for that route I have a class, but it fails when it makes a call to the ExactTarget library.
class sendexacttarget{
public function send($f3)
{
$client = new ExactTargetSoapClient($wsdl, array('trace'=>1));
}
}
I don't want to load the ExactTarget file in the index.php, because then it will load on every request. Ideally, I'd like to just load it here, in the send function. I've tried require() and include() but neither work.
I'm trying to include one file exacttarget.php and that file has a require statement calling soap-wsse.php which has another require statement calling xmlseclibs.php.
So first question: Is it possible to load these files from the controller and if so how?
The second part of the question is this. I am able to combine all of the PHP from those three files into one file and include from the index.php, but I have not been successful calling them as separate files. I have tried this:
$f3->set('AUTOLOAD','App/Controllers/;vendor/exacttarget/xmlseclibs.php;vendor/exacttarget/soap-wsse.php;vendor/exacttarget/exacttarget.php');
But it doesn't work.
I also tried this from another SO thread:
$f3->set('AUTOLOAD','App/Controllers/;vendor/exacttarget/');
$params = require 'vendor/exacttarget/exacttarget.php';
Doesn't work. This last bit of code I thought worked for a bit, but then it stopped working. I'm wondering if there is some caching going on?
In any case, if anyone can please help me include this library, even if on all pages I'd be very grateful. Also, this library I'm using is not available via composer so I don't think using composers autoload is an option.
Thanks.
The framework autoloader expects the following conditions in order to be working:
one class per file
each file named like the class it contains and located in a directory structure representating its namespace
See the documentation and this SO question for more details.
Since the external library you're trying to load doesn't meet those requirements, I suggest you three solutions:
1) require all the dependencies inside your controller:
public function send($f3) {
require('vendor/exacttarget/xmlseclibs.php');
require('vendor/exacttarget/soap-wsse.php.php');
require('vendor/exacttarget/exacttarget.php');
$client = new ExactTargetSoapClient($wsdl, array('trace'=>1));
}
2) create a service that will require all the dependencies and return an ExactTargetSoapClient instance:
// index.php
f3->SOAP = function($wsdl,array $options=[]) {
require_once('vendor/exacttarget/xmlseclibs.php');
require_once('vendor/exacttarget/soap-wsse.php.php');
require_once('vendor/exacttarget/exacttarget.php');
return new ExactTargetSoapClient($wsdl,$options+['trace'=>1]);
}
// controller
public function send($f3) {
$client = $f3->SOAP($wsdl);
}
NB: if the $wsdl and $options arguments are the same all other your app, you can even remove the function arguments and have a simple $f3->SOAP().
3) concatenate the three files into one single file properly named and have it autoloaded:
// vendor/exacttargetsoapclient.php
<?php
class ExactTargetSoapClient extends SoapClient {
// etc.
class WSSESoap {
// etc.
/**
* xmlseclibs.php
* etc.
Now set up the autoloader:
// index.php
$f3->AUTOLOAD = 'App/Controllers/;vendor/';
And you're done:
// controller
public function send($f3) {
$client = new ExactTargetSoapClient($wsdl, array('trace'=>1));
}
The AUTOLOAD autoloader doesn't support file names. It looks like you are using Composer so please use its autoloader:
require_once 'vendor/autoload.php'
Please adjust the vendor path relative (or use an absolute path) to the file which is requiring the autoload.php file.
I'm working on restructuring my Zend 1.12 project. I have a couple of view helpers:
OutputComplexForm.php
OutputDistributorsList.php
I put them in /application/views/helpers
Class names are
Zend_View_Helper_OutputComplexForm
Zend_View_Helper_OutputDistributorsList
As I understand if you have Zend_View_Helper prefix you don't need to add any configs to application.ini
Now, when I try to load any page (even those which don't use helpers) I receive error:
Message: Zend_Session::start() - /otms/vendor/zendframework/zendframework1/library/Zend/Loader.php(Line:134): Error #2 include_once(): Failed opening 'Zend/View/Helper/OutputComplexForm.php' for inclusion (include_path='/otms/application/../library:/otms/application/../library/phpseclib0.3.1:/otms/application/../library/Amazon:/otms/application/../library/USPS:/otms/application/../library/Composer:/otms/library:/otms/vendor/phpseclib/phpseclib/phpseclib:/otms/vendor/zendframework/zendframework1/library:.:/usr/share/php:/usr/share/pear')
Did I miss something?
UPD
I've found out that error occurred after calling function
$startedCleanly = session_start(); //line 482
in file /Zend/Session.php. After this call property Zend_Session_Exception::$sessionStartError contains described error message. I still don't see connection between starting session and initialising view helper.
For application-specific classes that you write - stuff that appears inside ./application/* - should typically not be in the Zend_ pseudo-namespace. Rather, they should be in the appnamespace, as configured in ./application/config/application.ini.
The default namespace is 'Application_', so a view-helper called MyHelper would typically be stored in the file ./application/views/helpers/MyHelper.php:
class Application_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
// do your stuff here
}
}
Note that the class name is upper-camel-case MyHelper and the method is lower-camelcase myHelper().
In your view, you can invoke your view-helper with:
<?php
$output = $this->myHelper();
// Do something with $output
With these conventions on namespace, class name, and file location/name, and invocation syntax, the View's plugin loader should be able find, load, and execute your view-helper method.
basically i am working on an update system inside codeigniter. My intention is to recurse copy directories from the extracted update file. Therefore I have created a helper file and auto loaded it so that i can test.
Within the helper file i have a function as following to copy the directory if it's a directory else copy the file:
new_helper.php:
if(!function_exists('directory_copy'))
{
function directory_copy($srcdir, $dstdir)
{
//preparing the paths
$srcdir=rtrim($srcdir,'/');
$dstdir=rtrim($dstdir,'/');
//creating the destination directory
if(!is_dir($dstdir))mkdir($dstdir, 0777, true);
//Mapping the directory
$dir_map=directory_map($srcdir);
foreach($dir_map as $object_key=>$object_value)
{
if(is_numeric($object_key))
copy($srcdir.'/'.$object_value,$dstdir.'/'.$object_value);//This is a File not a directory
else
directory_copy($srcdir.'/'.$object_key,$dstdir.'/'.$object_key);//this is a directory
}
}
}
After loading in the helper i attempt to access the function from within my controller, as following:
directory_copy($source.$file, $destination);
Furthermore PHP has been unkind and provided me with the following error: Fatal error: Cannot access self:: when no class scope is active in 'file location'.php
Quite new to Codeigniter therefore i'm not quite sure how i can use this function outside of my controller and have it work.
Does anyone know how i can get this to work?
Thank you.
EDIT:
I have included: $this->load->helper('directory'); in my controller.
Instead of creating a new helper, you should extend the Directory helper, as the "directory_map" function you're using is part of the directory helper.
Create a new file "MY_directory_helper.php" add your function, and save it in "/application/helpers", Then load the directory helper in your controller;
$this->load->helper('directory');
You should now be able to use this function
directory_copy($srcdir, $dstdir);
I'm loading two controllers from within my main controller and only the first one loads.
class App extends CI_Controller {
public function index() {
$this->load->library(array('../controllers/effects',
'../controllers/ingredients'));
$data['ingredients'] = $this->ingredients->get_all();
$data['effects'] = $this->effects->get_all();
$this->load->view('header');
$this->load->view('main', $data);
$this->load->view('footer');
}
}
I'm getting the error Message: Undefined property: App::$ingredients. If I switch the two path strings like this
$this->load->library(array('../controllers/ingredients', '../controllers/effects'));
then it says effects is undefined so it looks like it always loads the first controller but not the second. I tried autoloading them as well but I got an error like "nested function limit exceeded" or something. What am I doing wrong, how can I fix this?
put your library file in libraries folder inside CI
Now you can load your library in controller
$this->load->library('library_name');
to load multiple libraries in array
$this->load->library(array('library_name_1', 'library_name_2'));
or you can Auto-load Libraries in config/autoload.php
$autoload['libraries'] = array('library_name_1', 'library_name_2');
You should follow what the manual says about libraries.
You should put the library file on library folder and the load the library with
$this->load->library('name');
I'm using WordPress and I'm trying to write a plugin that uses a file from another plugin. The URL to get the file is correct. I'm able to include the file when I'm calling a static function from the class, it is saying that the class is not loading.
//loading the file
$url=plugins_url().'/nextgen-gallery/admin/functions.php';
include $url;
The filename is functions.php and in it is a class defined nggAdmin
However, when i call the function nggAdmin::create_gallery();, i get the following error:
Fatal error: Class 'nggAdmin' not found in /var/www/html/wordpress/wp-content/plugins/Product/addProductForm.php on line 27
plugins_url() gives you the url, e.g. http://example.com/wordpress/wp-plugins/, not the server path - http://codex.wordpress.org/Function_Reference/plugins_url
Use WP_PLUGIN_DIR instead - http://codex.wordpress.org/Determining_Plugin_and_Content_Directories