Adding Your own Code to the CI
'Super-Object'
You contribute to the process of building the 'super-object' as you write your own
code. Suppose you have written a model called 'status', which contains two class
variables of its own, $one and $two, and a constructor that assigns them values of 1
and 2 respectively. Let's examine what happens when you load this model.
The 'instance' class includes a variable 'load', which is a copy (by reference) of the
object CI_Loader. So the code you write in your controller is:
$this->load->model($status)
In other words, take the class variable 'load' of the current CI super-class ('this') and
use its method 'model'. This actually references the 'model' function in the 'loader'
class (/system/libraries/loader.php) and that says:
function model($model, $name = '')
{
if ($model == '')
return;
$obj =& get_instance();
$obj->_ci_load_model($model, $name);$obj->_ci_load_model($model, $name);
}}
(The $name variable in this code is there in case you want to load your model under
an alias. I don't know why you should want to do this; perhaps it's wanted by the
police in several other namespaces.)
As you can see, the model is loaded by reference into the Instance class. Because
get_instance() is a singleton method, you're always referencing the same instance
of the Instance class.
Saturday, February 26, 2011
Summary
Summary
The MVC framework is a widely used and very effective way of organizing a
complex website. CI uses it to help you sort out your own code, but it is also fairly
flexible about how it does so.
Try to keep in mind the two principles of 'loose coupling' and 'component
singularity' when you write your own code. Don't worry too much whether your
application conforms to the strict theory of MVC or not. The crucial thing is to
understand what the different types of file are, and how they relate to each other.
Then, you can decide whether to write your own code in library or model files, or as
helpers or plug-ins.
We've looked at the CI file structure, and seen how you can, if you want, inspect all
the CI code, but (thankfully!) you don't have to.
We did tinker with one of the original files: the config file, which holds critical site
information in one place to make it easier for us to upgrade or change later on.
We've seen the basic object structure of a controller, and used a simple constructor
to get some data from our config file and put it into a class property. And we've
dynamically passed information from a new controller we wrote, to a new view. So
far, the main thing CI has done for us is to encourage us to use a basic structure as
we start to define our site. As we go on, it will become clear just how important that
structure is.
Also, we looked a the way that CI's components pass data and control between
themselves. It's useful to understand this when you start to write your code.
Lastly, we looked at CI's own URL helper as a good example of a chunk of code, and
we wrote our own rudimentary 'menu' library class
The MVC framework is a widely used and very effective way of organizing a
complex website. CI uses it to help you sort out your own code, but it is also fairly
flexible about how it does so.
Try to keep in mind the two principles of 'loose coupling' and 'component
singularity' when you write your own code. Don't worry too much whether your
application conforms to the strict theory of MVC or not. The crucial thing is to
understand what the different types of file are, and how they relate to each other.
Then, you can decide whether to write your own code in library or model files, or as
helpers or plug-ins.
We've looked at the CI file structure, and seen how you can, if you want, inspect all
the CI code, but (thankfully!) you don't have to.
We did tinker with one of the original files: the config file, which holds critical site
information in one place to make it easier for us to upgrade or change later on.
We've seen the basic object structure of a controller, and used a simple constructor
to get some data from our config file and put it into a class property. And we've
dynamically passed information from a new controller we wrote, to a new view. So
far, the main thing CI has done for us is to encourage us to use a basic structure as
we start to define our site. As we go on, it will become clear just how important that
structure is.
Also, we looked a the way that CI's components pass data and control between
themselves. It's useful to understand this when you start to write your code.
Lastly, we looked at CI's own URL helper as a good example of a chunk of code, and
we wrote our own rudimentary 'menu' library class
Installing CodeIgniter
Installing CodeIgniter
One thing you don't need is your credit card: CI is completely free!
Once your server is set up, go to the CodeIgniter site at
http://www.codeigniter.com/ and download the latest version of the framework.
Version 1.5.3, the latest, is only 737KB when zipped, so the download doesn't take
that long
Unzip the folder, and install the CodeIgniter files in your web root folder. If you are
using Xampplite, this is usually the htdocs folder within the Xampplite folder.
The CodeIgniter index.php file should be in the root directory. The root folder is the
folder that you would point at if you navigated to the site—in this case, by accessing
http://127.0.0.1. Of the two minutes we need to set up the site, one minute is up!
Included with CI is a comprehensive user guide (in the user_guide folder). You'll
use this a lot. It is usually clear, and often goes into more detail than this book can.
So, try it if you get stuck.
When these files are on your machine, you can access them in two ways:
• As a URL—e.g., http://127.0.0.1
• Through the normal directory path: e.g.,
C:/xampplite/htdocs/index.php
You should be able to see the CI welcome screen by simply navigating to your URL
with the browser. It's that simple! The welcome page tells you that what you are
seeing is built by two files, a view and a controller.
One thing you don't need is your credit card: CI is completely free!
Once your server is set up, go to the CodeIgniter site at
http://www.codeigniter.com/ and download the latest version of the framework.
Version 1.5.3, the latest, is only 737KB when zipped, so the download doesn't take
that long
Unzip the folder, and install the CodeIgniter files in your web root folder. If you are
using Xampplite, this is usually the htdocs folder within the Xampplite folder.
The CodeIgniter index.php file should be in the root directory. The root folder is the
folder that you would point at if you navigated to the site—in this case, by accessing
http://127.0.0.1. Of the two minutes we need to set up the site, one minute is up!
Included with CI is a comprehensive user guide (in the user_guide folder). You'll
use this a lot. It is usually clear, and often goes into more detail than this book can.
So, try it if you get stuck.
When these files are on your machine, you can access them in two ways:
• As a URL—e.g., http://127.0.0.1
• Through the normal directory path: e.g.,
C:/xampplite/htdocs/index.php
You should be able to see the CI welcome screen by simply navigating to your URL
with the browser. It's that simple! The welcome page tells you that what you are
seeing is built by two files, a view and a controller.
Subscribe to:
Posts (Atom)