Part of the EllisLab Network
   
 
Default way for external classes/scripts inclusion
Posted: 12 May 2007 04:44 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  514
Joined  12-05-2006

why not make a common way for including external classes/scripts, even if they are a bit complex (several files/folders).

Namely set a folder in the application dir, let’s call it my_classes, where to put all the stuff, and to extend the php.ini include_path?

I usually do it with a pre-controller hook that looks as follows:
application/hooks/MyClasses.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class
MyClasses
{    
    
/**
     * includes the directory application\my_classes\ in your includes directory
     *
     */
    
function index()
    
{
        
//includes the directory application\my_classes\Swift\
        //change the ';'  on ':' to use in LINUX environment
        
ini_set('include_path', ini_get('include_path').';'.BASEPATH.'application/my_classes/');
    
}
}

application/config/hooks.php

//..
$hook['pre_controller'][] = array(
                                
'class'    => 'MyClasses',
                                
'function' => 'index',
                                
'filename' => 'MyClasses.php',
                                
'filepath' => 'hooks'
                                
);
//..
 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 12 May 2007 10:53 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  557
Joined  06-17-2006

Great idea. In that way you don’t have to have CI versions of all the other excellent libraries out there, and still have clean code.

 Signature 

CodeCrafter - Open Source Code Generation for CI

Profile
 
 
Posted: 12 May 2007 12:12 PM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1746
Joined  06-23-2006

Rather than the overhead of an entire Hook class to do an ini_set(), why not just stick that line in a config file that you autoload?
Or append it to index.php and remember that you did, if you need it earlier.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 13 May 2007 05:08 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  514
Joined  12-05-2006

@Crafter
Yeah, in fact we should never forget that CI is a tool we use to keep a clean MVC structure in our code, to keep common coding standards among developers working on the same stuff etc, but the core language is PHP, not CIC wink
If we have an external script made just by one file, we can put it in a plugin file or helper or what we want, but what if the script/class has several files calling each other like Swifth Mailer for example? Or some PEAR stuff?
To rewrite everything in CIC would mean to kill half of the great potentiality of PHP in terms of opensource scripts!

@Coolfactor
you right, maybe the hook is not necessary, we just need a line of code in the main index.php file wink

//includes the directory application\my_classes
//change the ';'  on ':' to use in LINUX environment
ini_set('include_path', ini_get('include_path').';'.BASEPATH.'application/my_classes/');

But the concept is the same: having a folder were to put all external scripts!

 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 23 May 2007 07:09 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  6
Joined  05-22-2007

Shouldn’t:

ini_set(‘include_path’, ini_get(‘include_path’).’;’.BASEPATH.‘application/my_classes/’);

Be:

ini_set(‘include_path’, ini_get(‘include_path’).PATH_SEPARATOR.BASEPATH.‘application’.DIRECTORY_SEPARATOR.‘my_classes’.DIRECTORY_SEPARATOR);

wink

 Signature 

Red Hat Enterprise Linux 4 - PHP 5.2.2 - Apache 2.2.4 - MySQL 5.0.37 - Zend Platform 3.0.2 - CodeIgniter 1.5.3

Profile
 
 
Posted: 23 May 2007 05:46 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  23
Joined  04-29-2007

Im a little confused why the load->scripts() method has been deprecated as this (or something similar that I have added in my mod_loader version of CI) would be an ideal way to include 3rd party packages/scripts without having to mess with ini_set etc.

This is the technique I use to integrate external packages such as SwiftMailer plus ones that I have created over time.

 Signature 
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77517 Total Logged-in Users: 29
Total Topics: 101528 Total Anonymous Users: 2
Total Replies: 544280 Total Guests: 263
Total Posts: 645808    
Members ( View Memberlist )