PHP Classes

File: code/includes/loader.php

Recommend this page to a friend!
  Classes of Junaid Hassan   PHP MVC   code/includes/loader.php   Download  
File: code/includes/loader.php
Role: Class source
Content type: text/plain
Description: loader.php
Class: PHP MVC
MVC framework that loads XML configuration files
Author: By
Last change:
Date: 10 years ago
Size: 2,715 bytes
 

Contents

Class file image Download
<?php

/* Class name : Loader
 * Inherited from :
 * Created by : Junaid Hassan (email : junaidhassanalvi@gmail.com , blog : junaidhassanalvi.wordpress.com)
 * Created On : 15-April-2103
 * Description : Created to load different views and controllers
 * Any one can write same functions to load files from includes and helpers or any custom folder
 *
 * Change Logs :
 *
 */

class Loader {

    protected
$catalog;

    public function
__construct($catalog) {
       
$this->catalog = $catalog;
    }

   
//jha-- load model
    //jha-- get models folder path form config
    //jha-- first check if file exists in model forlder
    //jha-- file name must be same as model name with a php extension
    //jha-- check if class is present
    //jha-- class name must be same as model name, prefixed with 'Model' keyword
    //jha-- return class object if exists
   
public function model($model) {
       
$config = $this->catalog->get('config');
       
$model_path = $config->base_path . '/' . $config->paths->models . '/' . $model . '.php';
       
$class_name = $model . 'Model';

        if (
file_exists($model_path)) {
            require(
$model_path);
            if (
class_exists($class_name)) {
               
$obj = new $class_name($this->catalog);
               
$this->catalog->set($model, $obj);
            } else {
                die(
'Required model is not found');
            }
        } else {
            die(
'Required model is not found');
        }
    }

   
//jha-- load views provided in $views array
    //jha-- get views folder path form config
    //jha-- check if file exists in views forlder
    //jha-- file name must be same as model name with a php extension
    //jha-- load file if present
    //jha-- return recived html views or output on screen according to $return parameter
   
public function view($views, $data = array(), $return = false) {

       
$config = $this->catalog->get('config');
       
extract($data);

       
$output = '';

        foreach (
$views as $view) {
           
$view_path = $config->base_path . '/' . $config->paths->views . '/' . $view . '.php';
            if (
file_exists($view_path)) {


                if (!
$return) {
                    require(
$view_path);
                } else {
                   
ob_start();
                    require(
$view_path);
                   
$output = ob_get_contents();
                   
ob_end_clean();
                }
            } else {
                die(
'Required view is not found : ' . $view);
            }
        }

        return
$output;
    }

}

?>