| Models |
| The models in Celeroo Frame are
optional. You can use them if you want to strictly follow the MVC
approach. If you decide to use models, they should be placed in the
models/ folder of your app. |
|
| What is
it? |
|
Celeroo Frame Models are PHP 5
compatible classes designed to work with your database. Typically
models provide abstraction layer for standard database operations like
add, update and delete. However you may decide to include in your model
also functionalities like search and sort.
The models in Celeroo Frame extend the class Basic and inherit its
methods add(), edit(), find() and delete(). Here is an example of
creating a model in Celeroo Frame: |
class MyModel extends Basic
{
function
__construct()
{
// Do
something if needed
// at the end call Basic constructor
parent::__construct();
}
}
The models in Celeroo Frame should be named after the database table
they connect to, without the table prefix, starting with capital letter
and in singular form (while tables should be singular+"s" even if this
is not gramatically correct plural form). For example a model that has
to work with table celerooframe_messages
should be
named Message.
(Like everything in Celeroo Frame, this is optional. If you choose not
to follow the naming convention, you can set the object property $this->tablename)
By doing this, you are ready to retrieve, insert, update or delete
records in it by simply calling the parent methods. (Check the
Guestbook MVC example). You don't need to implement the methods in your
objects, but if your data needs some kind of pre-processing you can
extend them or call them internally. |
|
| Loading
the models |
| The models in Celeroo Frame are just
classes. Unlike other frameworks, Celeroo Frame does not require a
special mechanism of loading the models. You just need to create an
instance of the model in your controller and assign it to a variable of
your choice. |
Example:
$model=new MyModel();
$model->do_something();
|
|
| Connecting
to the database |
All models in Celeroo Frame are
connected to the database by default. You can access the database
object as:
|
|
| Views |
The views in Celeroo Frame are web
pages or pieces of web pages written in HTML, XHTML and CSS. Your views
can and most probably will contain pieces of PHP code. Celeroo Frame
does not use a template language, because PHP itself is designed for
such usage.
Your views can have any extension, but Celeroo Frame recommends using
.htm or .html |
|
| Loading
the views |
Again Celeroo Frame uses the
simplest and most native approach to connecting the Model, Controller
and View layers. Celeroo Frame does not require specific loading
interface. You simply require your views in your controllers. This
allows you to use all the variables available in the controller in your
view directly, without time-wasting loading.
Example:
require("views/myview.html");
|
| |
| Loading
multiple views and views within other views |
| Since the views in Celeroo Frame can
use PHP code, you are free to include views within views. Typically, a
complicated page containing multiple views will consist of one main
view which requires the other views through PHP include
or require statements. Here is how this could look in
your controller: |
$view1="views/view1.html";
$view2="views/view2.html";
require("views/entirepage.html");
|
|
| Then mainpage.html will use include
or require itself to include $view1 and $view2 at the proper places. |
|
| The
Master View |
The default Celeroo Frame
installation improves the above structure even more by using one Master
View. Most web applications use same header, footer and CSS in all
pages. Because of that, Celeroo Frame by default loads a Master View in
the dispatcher (index.php).
Then each of your controllers only declares the view or views that are
included in your master. |
Example:
$view="views/register.html";
Again, each of your views can include unlimited number of other views. |
|
| Controllers |
The controllers are the procedural
part of your Celeroo Frame based apoplication. The controllers load the
appropriate models and views, call the functions, collect data and
prepare the output.
Unlike other frameworks, in Celeroo Frame we prefer to use an effective
approach over the canonical concepts of using objects everywhere.
The controllers in Celeroo Frame are matched to the action
URL variable in a Celeroo Frame installation that doesn't use URL
routing. If URL routing is switched on, your controller name will match
the first directory name in the URL. By default, the URL routing is
switched on. The controllers are placed in folder controllers/
in your folder structure. |
|
| A Hello
World Example |
Let's take a simple "Hello World"
example. If you call your controller hello.php, you will be able to
access it as:
|
http://yoursite.com/index.php?action=hello
|
Or in URL routed installation
|
http://yoursite.com/hello/
|
Your very simple Hello World controller doesn't even need a model or
view:
<?php
echo "Hello World";
?>
|
|
| Default
Controller |
One of your controllers will be
loaded by default when the visitor comes to your index page. This
controller should be called main.php but if you want to change that,
you can modify the dispatcher (index.php):
$_GET['action']=!empty($_GET['action'])?$_GET['action']:"main";
|
|
| Libraries |
Celeroo Frame comes with a set of
ready libraries and helpers. The libraries are files with simple
procedural functions using which you can develop your
applications/websites, etc. in simpler, faster and more organized way.
You can add and modify the libraries yourself.
If you want to use the libraries you can "autoload" them in the
autoloader - this is appropriate if you are going to use the libraries
across the application. If you think some of the libraries will not be
used often, you can simply include them in your controller.
Once loaded the functions within the libraries are available in your
controller and you can directly call them. |
|
| Helpers |
The helpers have similar purpose as
libraries, but the helpers are more complex OO classes. Some of the
functionality in the helpers is also available in the procedural
libraries but, in general, the helpers contain more advanced
functionality in logical units.
For loading the helpers use the autoloader or simply include/require
them in your controller. |
|
| The
Database Object |
Celeroo Frame comes with probably
the easiest to use database object. You will really forget about
writing same 2-3 lines of code again and again or fetching the database
response. Celeroo Frame database object is available in all your models
as:
and in all your controllers as:
Due to this $DB is a reserved variable name
and you should not use it elsewhere to avoid overriding the database
object. |
|
| The
$DB functions |
q() - Performs a database
query without fetching a result. Most appropriate for INSERT, UPDATE,
REPLACE or DELETE queries.
Example:
$q="DELETE FROM $T_USERS";
$DB->q($q);
aq() - Performs a query and returns an array
containing all the results from the query. The function is appropriate
when you expect to retrieve one of many rows from the database.
Example:
$q="SELECT * FROM $T_USERS ORDER BY id";
$users=$DB->aq($q);
Now in $users you have an array of associative arrays where the keys
match the fields from the database table. You can go through this array
with for, foreach or while.
sq() - Works the same way as aq() but
returns a single associative array. Most appropriate to use when you
want to get a single record of the table.
Example:
$q="SELECT * FROM $T_USERS WHERE id=5";
$user=$DB->sq($q); oq() -
Example:
$q="SELECT email FROM $T_USERS WHERE id=5";
$email=$DB->oq($q);
|
| |
|
 |
|
| Need Help?
Click
here to send us your queries on the framework
and/or the manual, and we will respond
promptly. |
| |
|