Web Design Forum: simple MVC framework question - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

simple MVC framework question Rate Topic: -----

#1 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 03 January 2012 - 03:06 PM

Mmmmm i have a question about building a personal simple MVC framework to use for my own projects

first thing is how do i make a simple super object kinda like codeigniter has where it has access to all objects where basicly i can do this

$Obj = Controller::Instance();


and using that simple instance from my super object i can access any methods with in all my classes using one single object like this

$Obj->view->load_view('signup_form');


you dont have to build it for me im just new to this and a few kind examples would be great :) exspecially from jock if he reads this, im sure he has worked with MVC quite abit but i welcome anyones examples
0

#2 User is offline   Mythriel 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 78
  • Joined: 15-June 10
  • Reputation: 4
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Web Developer

Posted 03 January 2012 - 07:45 PM

All MVC frameworks are built with the help of design patterns. I have built my own MVC framework and I can tell you that 1.it is a good way of learning solid programming and 2.it is fun. So regarding your problem, you want to use the Factory Design Pattern. http://en.wikipedia...._method_pattern. The factory design pattern deals with the problem of creating objects without specifying the exact class of object that will be created.
Also for your MVC framework you will need to look at other design patterns, and the ones that are mostly used and I have also used and are great for a framework are:
Front Controller design pattern - this is your entrypoint to the MVC, it could be index.php
Singleton design pattern - can be used for database object
0

#3 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 03 January 2012 - 09:41 PM

Thank you for the advice this is what i have so far with the framework, let me know what you think

index.php
<?php
//Load autoloader
require dirname(__FILE__).'/system/load.inc.php';
use System\Config\Config;
//Set up some configuration
Config::setBasePath(dirname(__FILE__));


system/load.inc.php
<?php
namespace System;
require_once __dir__ .'/Config/config.php';
require_once __dir__ .'/autoloader/autoloader.php';
use System\Autoloader\Autoloader;


system/Controllers/Controller.php
<?php
namespace System\Controllers;
abstract class Controller{
protected static $instance = array();
/**
*Creates and gives a new controller instance
**/
public static function Instance(){
$class = get_called_class();
if(!static::$instance[$class]){
static::$instance[$class] = new $class();
}
return static::$instance[$class];
}
}


system/Views/Views.php
<?php
namespace System\Views;
use System\Config\Config;
class Views{
/**
*Loads our view file into the browser
**/
public static function load($view,$data = array()){
$layout_path = Config::GetBasePath() . '/app/views/layout';
$view_path = Config::GetBasePath() . '/app/views';
/***Extract data from array***/
extract($data);
//Check if view exist
if(file_exists($view_path)){
  require $view_path.'/'.$view.'.php';
}
}
}


system/autoloader/autoloader.php
<?php
namespace System\Autoloader\Autoloader;
use System\Config\Config;
class Autoloader{
/**
Loads a class file using our autoloader
**/
public static function ClassLoad($class){
$_class_path = Config::getBasePath() . '/libraries';
//Check if class file exist
if(file_exists($_class_path)){
require $_class_path.'/'.'.php';
}
}

}

This post has been edited by webdesigner93: 03 January 2012 - 09:42 PM

0

#4 User is offline   Mythriel 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 78
  • Joined: 15-June 10
  • Reputation: 4
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Web Developer

Posted 03 January 2012 - 10:16 PM

yes ok, it is a start...but I think you don't need to give the controller static methods, u can just make an abstract application controller and all controllers extend that abstract class. The real fun will be when you start coding the router and the db class
0

#5 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 03 January 2012 - 11:00 PM

View PostMythriel, on 03 January 2012 - 10:16 PM, said:

yes ok, it is a start...but I think you don't need to give the controller static methods, u can just make an abstract application controller and all controllers extend that abstract class. The real fun will be when you start coding the router and the db class

1 question i have this error in my controller file Fatal error: Cannot instantiate abstract class System\Controllers\Controller in C:\xampp\htdocs\mvc\system\Controllers\Controller.php on line 13 any ideas?
0

#6 User is offline   Mythriel 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 78
  • Joined: 15-June 10
  • Reputation: 4
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Web Developer

Posted 04 January 2012 - 10:05 AM

well you can't instantiate an abstract class.

Quote

Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations which all objects that implement the protocol must support
- wikipedia. You can read more about them, but having several abstract classes which other classes in your framework inherit is a good design.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users