How to use core php into Codeigniter controller files? - php

I would like to use http://demo.hazzardweb.net/imgPicker/docs/. I have downloaded all the files.
How can I convert it into a Codeigniter third party library or how can I call this class file in a Codeigniter controller? Is it possible to call the core php files directly in a controller to achieve this?
For example:
require dirname(__FILE__) . '/ImgPicker.php';

You can create a new library for your project.
Your library classes should be placed within your application/libraries directory, as this is where CodeIgniter will look for them when they are initialized.
Be sure that:
File names must be capitalized. For example: ImgPicker.php
Class declarations must be capitalized. For example: class ImgPicker
Class names and file names must match.
Your ImgPicker class file
Classes in general should have this basic prototype:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ImgPicker {
// all stuff that your php library does
public function some_method()
{
. . .
}
}
Then from your controller (or model) you can load your library with $this->load->library('someclass'); and you can pass vars to the constructor dynamically like so:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('ImgPicker', $params);
You can find a lot of useful information how to create your own libraries (or convert existing php classes to CI libraries) at CI documentation.

Related

Where should I require vendor/autoload.php in CodeIgniter?

I want to add some code that will run for every controller. Adding the code to CodeIgniter's CI_CONTROLLER class seems unconventional.
Where is the right place to include code you want to run for every controller?
Here is the code:
require_once 'vendor/autoload.php';
$bugsnag = Bugsnag\Client::make("my-secret-key-is-here");
Bugsnag\Handler::register($bugsnag);
These classes both come from a dependency installed with Composer.
I suspect I should create a helper, and include it in application/config/autoload.php. But new to CodeIgniter, so not sure of conventions.
Edit: I am using CodeIgniter 3.1.6.
If you want to execute arbitrary code at different points in CodeIgniter's life cycle, you can use the hooks feature.
Official Documentation:
https://codeigniter.com/user_guide/general/hooks.html
1. Enable hooks
Go to /application/config/config.php.
Search for enable_hooks and set it to true: $config['enable_hooks'] = TRUE;
2. Add desired code to CodeIgniter's hook file:
Go to /application/config/hooks.php.
Choose the desired lifecycle to hook into (see doc link above for a list)
Add code to the lifecycle, e.g. $hook['pre_controller'] = function(){... your code goes here ...}
For this question's example, my hooks.php looks like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// This is the code I added:
$hook['pre_system'] = function(){
require_once 'vendor/autoload.php';
$bugsnag = Bugsnag\Client::make("my-client-key");
Bugsnag\Handler::register($bugsnag);
}
?>
I would just extend the Controller Class.
See "Extending Core Class":
"If all you need to do is add some functionality to an existing library - perhaps add a method or two - then it’s overkill to replace the entire library with your version. In this case it’s better to simply extend the class."
...
"Tip: Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core."
class MY_Controller extends CI_Controller {
....
}
Any function you put inside would be added to the core, otherwise, if you use the same name as an existing method, it would replace just that one method.
You'd name it MY_Controller.php and put it inside application/core/, where it's picked up to override CI_Controller automatically.
If you are extending the Controller core class, then be sure to extend your new class in your application controller’s constructors.
class Welcome extends MY_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function index()
{
$this->load->view('welcome_message');
}
}
Looks like you could also use a pre_system or pre_controller hook as described here:
https://www.codeigniter.com/user_guide/general/hooks.html
Assuming it's CodeIgnitor 3.X
Go to application/config/config.php and change:
$config['composer_autoload'] = FALSE;
to
$config['composer_autoload'] = TRUE;
just below the above line include
require_once APPPATH.'vendor/autoload.php';
Or in your controller include
require_once APPPATH.'vendor/autoload.php';
From How to use composer packages in codeigniter?
Go to application/config/config.php and set $config['composer_autoload'] = FALSE; to TRUE:
Enabling this setting will tell CodeIgniter to look for a Composer
package auto-loader script in application/vendor/autoload.php.
As a result you need not to call require_once 'vendor/autoload.php';.

Load external library in codeigniter

How can I access the getid3 class and use it in my controller?
project
/application
/controller
/libraries
/getID3
/getid3
getid3.php
/model
/views
Use CodeIgniter's built in constant, APPPATH. (Because code is not written in codeigniter's syntaxes)
`require_once(APPPATH.'libraries/getID3/getid3/getid3.php');`
If this library is codeigniter's built in library then you should use.
$this->load->library('libary name');
Regarding Your File Structure Codeigniter has the solution for it:
project
/application
/controller
/libraries
/getID3
/getid3
getid3.php
/model
/views
Now To call the Getid3.php the library you need to add this below code in the controller.
$this->load->library ( 'getID3/getid3/getid3', '', 'getid3(you can add any name you want' );
Now to Use this:
$this->getid3->your_function($data);
Please Note that Getid3.php must start with the capital letter.
From CI Documentation
you can load library by doing
$this->load->library('getID3/getid3/Getid3');
As explained in documentation if you have file located in a subdirectory like
libraries/flavors/Chocolate.php
You will load it using:
$this->load->library('flavors/chocolate');
You COULD just do a require_once on the main GetID3.php file
require_once(APPPATH.'libraries/getID3/getid3/getid3.php');
but there are better, more cleaner ways, to manage this third party dependency.
Instead of directly requiring the GetID3.php file, I recommend creating a wrapper class for the library. Creating a wrapper class affords you the ability to extend/overwrite the getid3 library and provides a cleaner implementation by allowing you to do things the "codeigniter way".
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class GetID3 {
function __construct ( $options = array() )
{
require_once( APPPATH . 'third_party/getID3/getid3/getid3.php' );
}
public function __get( $var ) { return get_instance()->$var; }
}
Doing it this way provides a cleaner interface to work with and allows for a more scalable approach to managing the third party dependency. Also, not the directory structure. Here, we are saving third party dependencies to the third_party directory while saving the wrapper class to libraries/GetID3.php.
Once you've implemented it this way, you can load the library as you normally would:
$this->load->library('GetID3');

Common functions for codeigniter custom libraries

I need to create custom libraries for my Codeigniter 3 application that have common functions used by all my custom library.
How to use function only in one place?
What you appear to need is a helper file, not a library.
Create a file in application/helpers directory
Eg file : mycommon_helper.php
Contents as :
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
function my_function($param1) {
printf('Hello %s', $param1);
}
Include this in the autoload file as below
$autoload['helper'] = array('url','html', .....'mycommon'); // Note _helper is omitted
After this you use the my_function($param1) anywhere in your code
$sayIt = my_function('user2473015');
You can create new custom libraries.
The library classes created by you should be placed in your application/libraries folder.
Classes should have basic prototype like this (here the name Myclass as an example):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Myclass {
public function my_function()
{
}
}
/* End of file Myclass.php */
From within any of your Controller functions you can initialize your class using the standard:
$this->load->library('myclass');
$this->myclass->my_function(); // Object instances will always be lower case

CodeIgniter & Smarty - can't change template dir - bridging smarty class doesn't overwrite setTemplateDir()

I am trying to add Smarty to CodeIgniter, here are the steps that I did:
Downloaded CI
Downloaded Smarty and put its content in 'application/third_party/smarty' folder
Created 'smarty.php' file in 'application/libraries'
Created 'templates' & 'templates_c' folders inside 'application/views' folder
Created simple 'test.tpl' file in 'application/views/templates' folder
Opened 'autoload.php' in 'application/config' folder and added:
$autoload['libraries'] = array('smarty');
Inside a controller wrote $this->smarty->display('test.tpl';
And I get this error - Message: Unable to load template file 'test.tpl'
After some debugging I have noticed that my Smarty template dir is set to default, and not the one that is specified in the bridging class ('application/libraries/smarty.php')
Here is the bridging class content:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'third_party/smarty/Smarty.class.php');
class CI_Smarty extends Smarty {
function __construct()
{
parent::__construct();
// NOT Changig this for some reason
$this->setTemplateDir(APPPATH.'views/templates/');
$this->setCompileDir(APPPATH.'views/templates_c/');
}
}
I think the CI_Smarty class isn't being executed for some reason, thats why my template directory is set to default
also note that if I go directly into Smarty.class.php and change the template directory manually - everything will work
Figured out the problem - bridging class should have been in 'system/libraries' and not in 'application/libraries' folder.
The problem is that you need to load ci_smarty library which extends smarty, not smarty directly.
$autoload['libraries'] = array('ci_smarty');
In your controller,
$this->ci_smarty->display('test.tpl');
Important
Please note that all native CodeIgniter libraries are prefixed with
CI_ so DO NOT use that as your prefix.
Try to change library name to custom_smarty instead.
Last but not least
Don't touch the system directory for any reason, Never ever. It is not the best practice. You can easily create new one or extend the existing libraries in your application/libraries directory.
Hope it will be useful for you.
Futher, if you like or need to use as
$this->smarty->display('test.tpl');
First, please CLONE the 'ci_smarty' object into 'smarty' on the Controller __Consructor() function
$this->smarty = clone $this->ci_smarty;

yii framework : How to access a class inside a layout(/protected/views/layout) file?

I have a class with some static helper methods. I want to use these methods from the layout file. These methods contain some common code used by all layout files. Even putting the class in 'components' folder does not solve the problem.
You have to import the class before you can use it:
Yii::import('ext.components.YourClassName');
$class = new YourClassName;

Categories