Load external library in codeigniter - php

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

Related

Change default controller directory of Codeigniter

I want to have two folders where save codeigniter's controllers:
/application/controllers
/application/buckets
i'm a order paranoic person and i want to separate two types of my controllers.
In bucket folders the structure app was this:
/application/buckets/example/index.php
/application/buckets/example2/index.php
/application/buckets/example3/index.php
¿Maybe extending the router class?
A working example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Extended the core Router class to allow for sub-sub-folders in the controllers directory.
*/
class App_Router extends CI_Router {
function __construct()
{
parent::__construct();
}
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
if (file_exists(APPPATH.'buckets/'.$segments[0].'/index.php'))
{
$this->set_directory(APPPATH.'buckets/'.$segments[0]);
$this->set_class(ucfirst($segments[0]));
$this->set_method(isset($segments[1]) ? $segments[1] : 'index');
return $segments;
}
}
}
You can use Hierarchical MVC(HMVC) with Codeigniter to accomplish this.
For reference, see Modular Extensions - HMVC
You may want to look into parent-child controller ...one extending another. To be more clear you can make as many controller you want.
I Agreed with #Brian Gottier : "what does changing their location do?"
You can perform anything if you have core functionalities in your hands.
You can play around with hooks (CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page.)
Create "Base"/"Admin"/"Public"/"XYZ" Controllers in
application/core/MY_Controller.php
and keep rest of your controllers in same application/controller folder
MY_Controller is a basic core library extension. Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library.
All we have done here is create a base class that all of our Controllers and "controller types" will inherit. Anything we put in here and assign to $this will be available to anything that extends this class.
Base Controllers are a nice simple way to give you global data, logic and shared code which can be specific to a certain part of your site. They can do all sorts of crazy stuff which I will leave for you to think about.
I Hope this help.

laravel 5 Class implements Iterator

I am trying to integrate an existing library into Laravel5 which itself is not namespaced and uses its own classes in subfolders using require.
I have placed it at 'app/API/libname/mainlibclass.php'.
with sibling directory at 'app/API/libname/toolkit' which contains the classes the library uses.
Calling from a Laravel controller I am unable to create the class using a require statement (correct?) before
$objectinstance=new Mainlibclass();
so in the main Laravel app I have
use app/API/libname/Mainlibclass
then later the usual
$objectinstance=new Mainlibclass();
In the existing library and each of its own used classes I set
namespace app/API/libname
and 'use' where needed.
I now have no class not found but one of the files uses 'implements Iterator' - I am getting error Interface 'App\API\libname\Iterator' not found.
Try adding \ in front of that so it looks like this:
class ABC implements \Iterator {
Edit:
I think it would be better practice to keep external non-psr-4/0 libraries untouched (for easier update if needed in future) and outside of app/ directory.
You could use composer classmap autoload feature for this.

How to use core php into Codeigniter controller files?

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.

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;

Sharing Joomla views across extensions

I created my own Joomla Library which holds a few utility functions and some views which should be shared with other extensions that need the same views.
Inside one extensions (Redextension) view all I do is call:
JLoader::import('mylib.views.objects');
The objects view is just a regular view:
defined('_JEXEC') or die;
class RedextensionViewObjects extends JViewLegacy
{
function display($tpl=null) {
...
The question is how would I go about abstracting the Redextension prefix because that will not work if I use that view in another extension that is called Blueextension.
Same goes for Models. The extensions have the same DB structure and functionality, so it would be great if I could share that within a library.

Categories