Welcome Guest, Not a member yet? Register   Sign In
include "" vs $this->load->library()
#1

[eluser]Dave Stewart[/eluser]
Hello,

New to the ways of CI, I'm aiming to adopt best practices and attain a high belt in CI-fu!

From what I can tell from the documentation, include is now effectively redundant because any classes or functions are loaded dynamically using $this->load->class/library/helper/etc().

But are there any situations where you would use include? For example your own external functions, or should I look at converting all my classes and functions to CI Libraries and Helpers?

Would I still use includes in views (as I saw someone else do), or should I let the controller handle everything, and only have plain old vanilla HTML in the view files?

Thanks for reading,
Dave
#2

[eluser]xwero[/eluser]
The use of include is not forbidden but using the load methods prevents you from including something more than once. This can be an advantage or a disadvantage based on the code you want to include. For instance i had to change the fpdf library to make it possible to create several pdfs in a loop. But the change isn't that earth shattering and i think that goes for most libraries out there.

The changes are needed because CI instantiates the library on loading which means in order to start out fresh you need a method to clean the previous setting first instead of creating a new instance. It's a manner of working. Wiredesignz has mentioned a simple solution for this; put the library in a helper file. These are not instantiated on loading because they are supposed to contain only functions.

I don't know what you want to include in a view but i guess i will be partials and for those you can use the load->view method.

So basically working with the load methods is easier most of the time.
#3

[eluser]Yash[/eluser]
yup agreed with xwero
#4

[eluser]Dave Stewart[/eluser]
Hi xwero,

Thanks for that insight. Yes, as you said, using load->view() appears to be the more OO solution in most cases.
I think as I get more used to CI and probably start to do more projects using it, my own framework of functions and classes will most likely be integrated within my own application folder.

Regarding my own application folder, is it possible to create a global library folder outside both the "system/application/libraries" and "system/libraries" folder?

My reasoning being that it would be really useful to be able to have a single (global) folder for ALL my CI projects on localhost, which I can then just copy verbatim to any remote hosts I work on, rather than having to copy stuff over for EVERY single CI project I start.


I saw here that Elliott Haughin moved his application folder outside of system, but I can't get this to work without an error. Any ideas?


Cheers,
Dave
#5

[eluser]xwero[/eluser]
The way i work most of the time is to create a codeigniter directory somewhere above the public root and that directory hold the CI framework and all the application folders. Most of the application folders are named like the domain to make it easy on myself. The system directory is renamed to shared because i see the system as the whole of CI + application + public files.

In the shared library i create the directories models and views for files that can be used in several applications. I put libraries that can be used in several applications in the shared/libraries. this way i can benefit from the CI extending libraries functionality. Of course this is not possible with the extended libraries they have to be copied until CI makes it possible to change the paths.

So it's not really necessary to create a folder outside the system. Even your old sites could link to the shared|system/libraries folder files to keep the copying to a minimum.
#6

[eluser]Dave Stewart[/eluser]
Thanks for the info. I'm sure it will become clearer as I start development proper.

With regards to moving the app folder (I must admit, I personally like this approach) how do I configure CI to know about this?

I had a look in the front controller file index.php and it looks like it should work (to just move the application directory up one level) as it's setting first a root-level application folder, then falling back to a system-level application folder... but when I run my controllers and views, variables are now longer bring passed from the controller to the view correctly.

Any clues!?

Thanks,
Dave
#7

[eluser]xwero[/eluser]
You have to add the full path to the $application and $system variables in the index.php file.

i find it strange your variables are not passed on to your view because they don't need to know the paths.
#8

[eluser]Dave Stewart[/eluser]
How do I handle local and remote locations?

Usually I detect the $_SERVER vars and do a switch/case from there.
It feels a bit dirty to be doing so much editing of the index.php file, but I guess it's only one file.

Cheers,
Dave
#9

[eluser]xwero[/eluser]
What do you mean by local and remote locations? Are you planning to load files from other servers?? Relying on $_SERVER vars is very tricky because most of them can be manipulated.

If you are talking about developer and live server you should check the automatic base_url solution
#10

[eluser]Dave Stewart[/eluser]
[quote author="xwero" date="1214417211"]If you are talking about developer and live server...[/quote]

Yup - that's what I'm talking about. For me there's usually 3 servers:

- development server (localhost)
- staging sever
- live server

So a switch/case or an associative array usually works best.

Also, apart from the base_url there's also the application paths to consider (if they have to be absolute as you said)

Case closed Smile

By the way, what did you mean by $_SERVER vars (in this case $_SERVER['HTTP_HOST']) can be manipulated? By whom, and to what end?
Cheers,
Dave




Theme © iAndrew 2016 - Forum software by © MyBB