Welcome Guest, Not a member yet? Register   Sign In
extending codeigniter - best practices?
#1

[eluser]zauber[/eluser]
Hello!

Sorry guys/gals, but this is going to be a long post. (But hey, it's my first - be nice to me!)
Where I work, we've only quite recently begun looking into moving away from code-generation and hand-coding based PHP to using some OO PHP framework.

I've been looking around at various frameworks, and concluded that we need something along the lines of a "glue-framework" - something that isn't too restrictive in terms of extending with our own existing code libraries. CodeIgniter and the Zend framework seem to fit the bill, and frankly the Zend framework seems overbearingly complicated. CodeIgniter, however looks very promising.

Having looked into codeigniter a bit more, however I have become concerned regarding how to go about reusing our existing code libraries.

My first question is: What, really, is the difference between libraries, helpers and plugins. If I were to "ignite" some existing code-libraries, would I make it a helper, a library or a plugin?

We already have a number of existing code libraries, including a RBAC library, password and authentication managers, html-component renderers, a form-rendering-validation framework etc. These libraries have their own folder-structure, and have requirements with regard to configuration variables (database-settings mostly generally), where the supporting code is located etc.

My second question: My hope is that I will not need to rewrite any of the existing libraries specifically for CI (The problem with lock-ins is the reason we're changing our practices in the first place). Hence my notion is that I will be able to simply write thin "adapters" between CI and the existing libraries. Any thoughts on this? Can anyone give examples? Am I mistaken in thinking this is the best route?

Generally speaking, we will be developing several applications. Preferably, we would use the same CI code-package for all applications. My notion then is to use a directory layout something like this:

Code:
/libraries
    /codeigniter (basic codeigniter-system folder, minus the application folder)
        /libraries
             (assuming libraries are the appropriate route, see my first question)
             MY_AuthenticationWrapper.php
             MY_RBACWrapper.php
             MY_PasswordmanagerWrapper.php
             MY_FormHandlerWrapper.php
        /helpers
        /whatnot
    (the following are our own libraries)
    /Auth      
        /Authentication
        /RBAC
        /Passwordmanager
    /FormHandler
        /widgets
        /js
/applications
    /applicationOne
         (typical codeigniter application folders
         /views
         /controllers
         /hooks
         (folders for things relating to our custom libraries)
         /somethingelse    
    /applicationTwo
         ...same thing
/www
    /applicationOne
         index.php (the index file that launches codeigniter)
         /media
              (media folder contains static resources served by apache)
              /css
              /js
              /gfx
              /whatever
    /applicationTwo
         ... same thing

With this layout, I can make www the document root in apache, and keep all libraries, even application specific code, outside the scope of apache.

My third question is: those of you who have used CI for large-scale projects, with several applications using CI + 3:d-party libraries - what are your thoughts on this layout? Is it possible and reasonably maintainable with CI? Do you have better suggestions.

Again, I apologize for the lengthy post. I feel it is important for me to be detailed, in order to get good answers, so I can make an informed decision regarding our development future.
#2

[eluser]nmweb[/eluser]
Quite lenghty Wink I might not be able to answer all of your questions but I'll give it a try.

Libraries are basically a coherent set of functions that work together. Helpers only aid you in your development and as such are quite close to being native php functions. They just save you work. Difference with plugins is that a plugin is basically one function, helpers have more.

Adapting existing code to CI using wrappers is a good way to go. If it's well-written OO code simply including the right files might be equally good but it's mainly a matter of preference in my opinion. It also depends on the code you want to use in CI, adapting your own session library with a wrapper to CI might be more easy than adapting a full-blown RBAC. Also there is a tutorial out there on how to use Zend Framework libs within CI that might be helpful.

I'd also look at http://ellislab.com/forums/viewthread/65749/ to see some modularization in CI. Seeing what you want to incorporate this should be a way to go about.

Keeping your application/system code outset of the webroot is of course advisable Smile
#3

[eluser]wiredesignz[/eluser]
From the user_guide:

CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder. You can create your own libraries within your application/libraries directory in order to maintain separation between your local resources and the global framework resources

Plugins work almost identically to Helpers. The main difference is that a plugin usually provides a single function, whereas a Helper is usually a collection of functions. Helpers are also considered a part of the core system; plugins are intended to be created and shared by our community.
#4

[eluser]zauber[/eluser]
Thanks for the quick and informative responses. From the tutorial on Zend + CI, it seems what I want to do is both possible and reasonably simple.

Regarding plugins vs helpers vs libraries: I have in fact read the user guide, but found I didn't quite understand the distinction. Still not sure I understand I'm afraid.

I guess what I am asking more specifically is this: Given an external code-library with a well defined api, under what circumstances would I make it a library, helper or plugin, and under what circumstances would I manage loading it myself in controllers (using require_once())? Even more specifically:

1. Is it ever appropriate to write your own helpers?
2. Are plugins only ever supposed to contain a single function?
3. Are libraries not intended to be distributed by the community, as opposed to plugins?

At the moment, it appears to me the only recommended option is to cram our full API into the single-function-wrapper of a plugin, which doesn't make a whole lot of sense. I'm sure I must be mistaken.
#5

[eluser]gtech[/eluser]
[quote author="zauber" date="1197580371"]

1. Is it ever appropriate to write your own helpers?
2. Are plugins only ever supposed to contain a single function?
3. Are libraries not intended to be distributed by the community, as opposed to plugins?

[/quote]

I think there is a grey area however I don't tend to get bogged down with whats what as CI is a loose framework and you can adopt a strategy to suit your needs. [url="http://www.jimohalloran.com/2007/09/06/how-i-use-codeigniters-mvc/"](usefull link)[/url]

I see libraries as classes and tend to be more substantial, they can have config variables and tend to have a group of methods to help you get a task done. helpers are just a set of handy functions that you might reuse throughout your code, e.g. a function that performs a simple regex on a form, if in doubt why not use a library.

1. if you want to! CI is a loose framework adopt your own strategy
2. usually, I personally have not used them so I am not best to comment.
3. libraries are distributed in the wiki [url="http://codeigniter.com/wiki/Category:Libraries/"]see here[/url], so I am not sure I understand the question. There is a directory (system\application\libraries) where you can put your own libraries in without interfering with codeigniters, you can even extend codeigniters existing libraries. Every application can have there own set of libraries.

maybee this link might help:

[url="http://www.derekallard.com/blog/post/giving-your-helpers-a-little-help/"]DrerkAllard.com[/url]
#6

[eluser]zauber[/eluser]
thanks gtek! I think it's clearing up a bit. I was mostly just being fearful that by somehow doing something wrong I would dig myself into a hole I'd have hell to get out of Smile I think for most our code, libraries are the way to go then. With respect to question #3, what I meant was that plugins are specifically named as "intended to be distributed by the community" which made me think "ah, 3:d-party libraries should be plugins... so libraries... must be intended as somehow non-third party".

Alright... so enough worrying about the minutiae - time to get hacking Smile
#7

[eluser]Michael Wales[/eluser]
One rule of thumb: always place your own additions into the application directory, never hack the core.

The application directory is missing some folders by default (I can't remember which, plugins, helpers, I think). But if you create the folder and place your addition there, CI will use it (I've already requested that EllisLabs include blank directories in future versions, to make this more apparent).
#8

[eluser]zauber[/eluser]
Cool! Good tip
#9

[eluser]gtech[/eluser]
happy hacking and welcome to CI, let us know how you get on!




Theme © iAndrew 2016 - Forum software by © MyBB