How to Initialize Your Own 'library' Classes

Originally, you couldn't make your own classes part of the CI 'super-object'. This was a problem, because it meant that your library code couldn't, for instance, access the database through Active Record, or use other CI libraries, and that became pretty limiting. Version 1.2 added the get_instance function that allows you to access the 'super-object'. See Chapter 7. You could include it in your 'library' or 'script' and then use the CI resources. Unless your new file was a functional script...

Designing a Better View

At this stage, you might ask why we are going through so much effort to serve up a simple HTML page. Why not put everything in one file For a simple site, that's a valid point but whoever heard of a simple site One of the coolest things about CI is the way it helps us to develop a consistent structure, so that as we add to and develop our site, it is internally consistent, well laid out, and simple to maintain. At the start, we need three common steps Update our config file to specify where the...

Interacting with Controllers

You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers. However, you can't call one controller from another, or call a controller from a model or library. There are only two ways that a model or a library can refer back to a controller Firstly, it can return data. If the controller assigns a value like this fred this- gt mymodel- gt myfunction and the function is set to return a value,...

The Test Suite

Remember those 'test blocks' in the delete function Their purpose is simply to detect if the function is being run 'for real' or for a test, and, if the latter, to make sure that it returns a value we can easily test. This is all because, at the end of the CRUD model, we have a 'self-test' suite. This is called by the test function in any controller it doesn't matter which one and performs generalized CRUD tests using a dummy table. First in the CRUD class there is a master 'test' function,...

Yes, But...What is CodeIgniter? What are Frameworks?

Shortly after programming was invented, someone noticed that it involved many repetitive operations. And shortly after that, someone else maybe it was Ada Lovelace, spanner in hand, adjusting Babbage's differential engine, or maybe it was Alan Turing at Bletchley Park decided to modularize code, so you only had to write certain chunks once, and could then re-use them. PHP programmers are used to writing separate chunks of code in functions, and then storing those functions in include files. At...

An Example of a CI Helper: the URL Helper

As an example of the way you can split your code up into neat, focussed, chunks, CI's URL helper contains a set of functions that help you to manipulate URLs. You load it like this And then, you can use it to find and return the site and or base URLs that you set in your config file You can also use it to create hyperlinks. We saw in the last section how you can access the hello function in the start controller, and pass it the parameter fred, with a URL like If you want your own code to create...

Machines Talking to Machines Again—XML-RPC

The Web 2.0 revolution is largely built on machine-to-machine interfaces, which allow mashups and APIs and all those good things. This is the basis of 'web services'. You can offer an interface to your site that allows other people to use it to do something for them. To give a simple example, if you set up a 'web service' that converts temperatures in centigrade to Fahrenheit, the client sends in a request with one parameter the temperature to be converted and the server returns the converted...

The File Upload Class

Sometimes, you want to allow users of your site to upload files. These may be text, or images, or more exotic file types like MP3 audio or MPEG video. This is a more complex process than the file downloads we just discussed, but CI s file upload class takes care of most of the work for you. It also looks after some of the security issues. However, you should always think twice before allowing anyone to upload files to your site, and you may want to protect the upload page to prevent...

Easy File Compression with the CI Zip Class

If you re moving around large files like images, you might need to compress them. CI contains a handy library for doing this. Let s take the photograph we ve already used waltzer.jpg. It s on our uploads folder. As always, you start by initializing the Zip class. Once you ve done that, you have to tell CI which file you want to zip, and create an archive to put it in. Then you use the read_file function to read it in and zip it, and the download function to download it to your desktop. this- gt...

CRUD: the Final Frontier

You need to write CRUD pages in almost every application. It seems simple, and logical, to automate the process of creating those pages They are tantalizingly standard and yet they have deceptively large numbers of possible variations. It s impossible to write one without starting to impose your own rules and assumptions on the user. Also, there is always a trade-off between covering more and more possible options on the one hand, and simplicity of use on the other. The more exceptions and...