Loading Please wait...

MVC Explained

Controller holds the business logic of an application. Its not recomended to put any presentation(HTML) code in the controller. one or more HTML File (with embeded PHP Codes holds the Visual representation or Presentation) which is rendered or loaded by the controller to show the visual elements while the data is passesd to the views through Variables (either through mmber variables or through an associative array). In the runtime before invoking the method the model is attached with the controller which binds Orm Maps with the Controller.

The V of MVC

view.png
Now we will discuss about the V e.g. View and Its friends (e.g. Layout , Param etc..). There are two types of views.
  1. General Views
  2. Shared Views
which can be either loaded or rendered from Controller or embeded as renderable or loadable into another View. Shared View is Shared accross Controller. e.g. It doesn't have any defined owner [[controller]] instead its owned by the project itself and available for all the [[Controller]]s to use it. On the other hand general views are Owned by a specific method of a Controller. a controller class have a set of methods and each method might have one or more views (Presentation Blocks). a view file name ends with an extension .view.php. all views are stored into project/home/apps/view/controllerName/methodName Directory. The default view of a method is a view whose file name starts with the method name itself. e.g. default view of a method named report of studentController Class will be named as report.view.php and will be located at project/home/apps/view/student/report/report.view.php the report method might have more Views. However the default view is autorendered at the end of method execution and the other views needed to be loaded or rendered by invoking $this->renderView() or $this->loadView() method.

View

This is a View which shows a Visual Representation Just like the bellow one
Hi! <b><?= $user ?></b><br />
<? foreach($marks as $sub => $mark): ?>
 You Have got <b> <?= $mark ?> </b> out of <span class="total">100</span> in <span class="sub"><?= $sub ?></span><br />
<? endforeach; ?>
Notice that There are embedded PHP Codes as well as variables and from where the Variables like $user. These variables come from The Controller.
<big>Now you might scream that there is No <html><head><title> and other Compulsury tags ?????</big>
but in a zigmoyd application you will see all those tags in the browser's output. actually View is treated as parts of Presentation Layer not the Complete Presentation Layer. this and all other Views are evaluted (doesnt maess it with eval() however zigmoyd doesn't use this function in any of its operation cause in some shared hosting eval() function is disabled.) and then their outputs are Concated and stored into a Valid HTML Page which is Called layout.

Layout

This is the Layout Page (the standered Layout which is shipped with zigmoyd will slightly differ from it However the Concept is still same)
<html>
  <head>
    <title><?= $params['title'] ?></title>
  </head>
  <body>
    <?= $params['content'] ?>
  </body>
</html>
here is another Embeded PHP Code <?= $params['content'] ?> in <body> Tag. all the view are evaluated and their output is concated and stored into $params['content'] variable which is injected into <body> tag. you have also noticed that there is another variable called <?= $params['title'] ?> so from where this comes ? thers is a file which holds common set of parameters (params) which holds the default value of all the params such as title, header, footer, meta, and you can put custom params on that file. First that file is loaded for the shake of default values of params. (However those values can be changed from the Controller latter on Runtime).

Params

This is a Param File (the standered Layout which is shipped with zigmoyd will slightly differ from it However the Concept is still same)
$params['title'] = "My Page Title";
First the Params file is loaded to get the default values of the params variable. so that $params['title] would get replaced by My Page Title

The C of MVC

Here does your Business Logic go.
//Class homeController ultimately inherits from zigController Class.
class studentController extends homeController{
  public function main(){
    //action logic for main( method here
  }

  public function report($id){
   $this->user = "Foo";
   $this->marks = 94;
  }
}
Firstly we are simply defining two member variables user and marks. after report method of a Controller is executed. internally $this->renderView('report'); is getting executed automatically. (e.g $this->renderView(theMethodName)). renderView() pulls all member variables from $this ([[Controller]] Instance) and passes them as local variables to the View. so that the view can access $this->user as $user and $this->marks as $marks;

If User doesn't Secify any method http://localhost/zig/student then its main() method is executed . e.g. it behaves like this url http://localhost/zig/student/main

In a controller initialization might be done through Constructor . But If you want to initialize through you need to remember two things.

However Its not recomended to use constructor to do initialization stuffs. rathar zigmoyd lets you use init() method. If you have a method named init() on your Controller that method is executed before other method's execution and after Constructor execution. and init() method have access to ORM Layers and also there is no hassle to call parent class's init() method.
If you have an init() method on project level Controller homeController Class that will effect all the Controller of that Projec.
init method can take arguments too init($projectName, $controllerName, $methodName, $args).

Siblings

Assume your business logic is too big (however you can separate/isolet/abstract jobs through different libs) its so big that every method is taking 100 lines of code and you have 10-20 methods on a controller. Then it will be a problem as 1000s line of code in one file.

Zigmoyd has a solution for this situation. That is each method is one file. you can have small methods in studentController Class in student.php. However you can separate big methods in a separate file (which is called Siblings). that will be resolved at the time of request.

for that open the Directory projects/home/apps/controller and create a Directory with the same name of Controller e.g. student here. and now go to projects/home/apps/controller/student and create a file with the same name of the method say report. so you create a file projects/home/apps/controller/student/report.php

and now write the student Sibling class there as the following one.

class studentSibling extends studentController{

}
and then insert the report method in this Class.
class studentSibling extends studentController{
  function report($id){
    //do the same stuff here.
  }
}
this report method works in the same way as it was in the studentController Class no codes needed to be changed.

The M of MVC

But in the real life Situation you wilp not simply assign values to member variables. rather you will get the values from the databes. So in Real life situation you will code something like.
$this->user = getUserFromDatabase();
//or
$this->user = getUserNameFromStudentTable();
In Zigmoyd you will code.
$this->user = $this->db->student->findNameById($id);
Isnt it simpler than others ? findColBySearch(value) here the $d is passed through report method.

$this->db is the Container of all Table Objects. here student is a Table level Object which represents the std Table. there might me other tables called teacher that will be accessible as $this->db->teacher. e.g. all tables are contained in $this->db Container Object.

and in every table Object you can execute find, fetch, set, etc.... methods that will access database while writting SQL queries on the fly. As you are not writtig the SQL Query in the action logic there is no chance of SQL Injection.

Now one question apears in your mind thats how to attach student or teacher table with the Controller. Thats the job of Models and ORMs

In Zigmoyd Database accessing is not a Direct operation it goes through a database abstraction Layer. which may be configured to use some alias names for tables or columns , Use some permission rules, filter the data before writting to the database, automatically invoke some trigger operations written in PHP with Zigmoyd. and many other Job.

This Layer is configured through some configuration Files called ORM Maps (which are parsed and serialized and Embeded into ORM Classes).

ORM Map

ORM Maps are Highly human readable textual configuration files that are stored into etc/conf.d Directory with orm.map.php extension. You dont need to bother about writting a configuration file from scratch. zigmoyd have Command line tools for that. However its worth to know the structure of that file cause without knowing that you cant edit that file maually.

This is an example ORM Map. where I am mapping two Tables std with alias name Student and std_score with alias Name Marks. and std Table have a Column called syd_name which I'll use with an alias name Name and std_id column is auto Incremented so Its readonly. and you can see how the validation Filters are applied. and these two shares same Connection attribute dataConn (dicsussed in the next section). Zigmoyd commander is capable to generates these coiguration files by itself even with the filters and Error messages.
connection as dataConn{
}
table std as Student{
    readonly std_id as Id{
    }
    writable std_name as Name{
        required:       Student's Name Must not be left Blank
        string:         Student's Name Must be a String
    }
}
table std_score as Marks{
    readonly id as Id{
    }
    writable student_id as StudentId{
        required:    StudentId Must not be left Blank
        int:         StudentId Must be an Integer
    }
    writable subject as Sub{
        required:       Subject Must not be left Blank
        string:         Subject Name must be a valid string
    }
    writable marks as Marks{
        required:       You must enter the marks obtained by the student
        int:            Marks Obtained by the Student Must be a string
    }
}

Connection File

Now Lets see whats in the Connection attributes. In the Connection Block we are specifying dataConn as the commection which means use connection attributes from a file dataConn.con.ini.php which is stored in etc/conf.d Directory in your Project's Directory.
driver = "mysql"
database = "manual"
host = "localhost"
user = "root"
psw = "123456"
as the connection parameter is separated in another file more than one orm map can use the same Connection file.

ORM Classes

Zigmoyd parses ORM Maps and generates ORM Classes. Each table has two Classes.

so if the above mentioned ORM Map's name is studentOrm.orm.map.php ORM Classes for this Map will be created in a file Called studentOrm.orm.php which will be stored in projects/home/apps/model/orm Directory. and now the question is what will they contain.

In studentOrm.orm.php there will be two Classes.

you execute all find, fetch, set, etc.... methods on an object of the Table level Class. and whenever you fetch some data through select like query that returns a row or more than one column e.g. findAll() etc..... the result is provided to you by the RowSet Object.

actually $this->db->student is an Instalce of Student Class. Internally something like $this->db->student = new student(); is done automatically by Zigmoyd while binding the Model wicth the Controller.

as these are just PHP Classes you can add your own custom methods , Triger to customize activity of the Table level and row level Class.

Models

Models are Simple Configuration files (written in PHP) that attach's ORM maps with your Controller.
<?php
class studentModel extends manualModel{
        public function attachOrmMap(){
                parent::attachOrmMap('StudentOrm');//attach an Orm Map on file StudentOrm.orm.map.php
                //you can attach More ORM Maps in the Same way
        }
}
?>
If the ORM Class for this ORM Map has already been generated it loads that else generates the ORM Classes by parsing the ORM Map.

Lifecycle of a Zigmoyd application

as the Url Scheme is mentioned there Zigmoyd reads The Following Items from the Url if the url is http://localhost/zig/student/report/52 (assuming Project name is home which is in project/home directory)

There are a lot more Internal Steps Skiped. the Sequence is not 100% correct to keep it simple.


Generated on Mon Oct 27 23:51:59 2008 for zigmoyd.kdevelop by doxygen 1.5.6