Thursday, September 12, 2013

Creating A Simple PHP MVC Framework Part Four (smarty version)

 Written by Lorenzo D. Alipio (open source developer) 


In previous tutorial (part three), we managed to create crucial files for our simple MVC framework. In this series (part four), we will be creating the core files of our application. These core files will be base controller, base model  classes. For clarification, we will not be creating a base view class. The reason is that the view will be utilizing the smarty and there is no need to create an abstract method for it.

In part one, our projectcreator.php generated our base file structures as shown below.

htdocs
    + mvcproject
          + application
           |     + controller
           |     + model
           |     + view
           +temp
           +web
           |    +themes
           |         +default
           |         +themes_compile
           |         +themes_configs
           composer.json *for later
           smarty.zip *if connected to the Internet

Inside the mvcproject directory, create a new directory called vendor, and then create these additional directories and files.

        vendor/core
        vendor/core/controller/Controller.php
        vendor/core/model/Model.php
        vendor/core/view/View.php
        vendor/smarty/smarty
        vendor/modules/

our mvcproject directory should now look like this

htdocs
    + mvcproject
          + application
           |     + controller
           |     + model
           |     + view
           +temp
                +vendor
                 |    +core
                 |     |   +controller
                 |     |    |  Controller.php
                 |     |   +Model
                 |     |   |    Model.php
                 |     |   +View
                 |     |   |     View.php
                 |    +modules
                 |    +smarty
                 |    |     +smarty 
           +web
           |    +themes
           |         +default
           |         +themes_compile
           |         +themes_configs
           composer.json *for later
           smarty.zip *if connected to the Internet

If you already downloaded the smarty zip file, unzipped the file and then move these files to vendor/smarty/smarty directory.

          +smarty
              +smarty
                   +plugins
                    +Smarty_info
                     +sysplugins
               | debug.tpl
               | Smarty.class.php
               | SmartyBC.class.php

Let's create our base controller file.

filename: vendor/core/controller/controller.php


<?

/*
 * Copyright Lorenzo D. Alipio 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * Controller.php
 * namespace application\controller\;
*/



Abstract Class Controller {

/*
 *
 * @model use model
 * @view use view
 *
 */
protected $model;
protected $view;

function __construct() {
        
  $this->model = Ini_object::instance('ModelClass');
  $this->view = Ini_object::instance('View');
}

/**
 * set contract between main controller and the application controllers. Must have
 */
abstract function index();
}


?>

We create our base model class.
filename: vendor/core/model/BaseModel.php

<?
/*
* 
* Copyright Lorenzo D. Alipio 
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* 
* BaseModel.php
*/
//Use_Class::autoload(PDO);

Abstract Class BaseModel{

public function __construct(){}

Abstract function Content();
 
}
?>

I commented the PDO class, because it is not included in this tutorial. However, database directory can be added inside the vendor directory, and then save the pdo class there, and then just define it as a constant in the bootstrap.php

This concludes the part four of this tutorial series. For part five, we will be integrating the twitter Bootstrap front-end framework with our simple PHP MVC Framework. Stand by its coming soon..

No comments:

Post a Comment