I love building my own framework from scratch, The most perfect framework to me would be something like so:
File Structure
- .htaccess
- index.php
- /system/
- /applications/
- /libaries/
- /requirements/
My index.php file would take care of defining certain constants like so:
define('ROOT_DIR',str_replace("\\","/",dirname(__FILE__)));
define('SYSTEM_DIR', ROOT_DIR . "/system");
define("FRAMEWORK_VER","1.0.0",true);
require_once SYSTEM_DIR . "/startup.php";
Simple as that, But within startup.php I would load the following criteria's for my framework:
- SPL Autoloader, to load general classes / interfaces etc.
- Load error checking class and start capturing output, encase some hook pre-sends
whitespace
- Create a registry objects to store objects like so:
Registry::add('Config',new Configuration_Loader());
Registry::get('Config')->system->host;
- Clean all user input such as $_GET,$_POST,$_COOKIE with htmlentites
- Load the route class and generate a route,
- All this is for is to check for
controller/method/param1
or ?c=test&m=test
etc.
- Once the raoute is bein found, I will send the route of to the Loader, witch in turns loads the required classes or throws a 404* error page.
when it comes down to the controller, as I have a registry object with a global scope i usually just do the following!
abstract class Controller
{
public function __get($object)
{
return Registry::get($object);
}
}
and then do like so:
class Controller_test extends Controller
{
public function test()
{
$this->View->title = "Hello World";
$this->output->send($this->View->compile("test_page"));
}
}
I tend to never create a template system as PHP itself is a great template system, I usually compose my view system with 2 objects!
First object is the View
class witch just has getters and setters for the template data, and then ViewCompiler witch loads the template within the scope of its class like so
class View
{
//...
public function compile($template_name)
{
$Data = ViewCompiler($template_name);
}
//...
}
Then within ViewCompiler
:
class ViewCompiler
{
public function __construct($root,$data)
{
ob_start();
require_once TEMPLATE_DIRECTORY . $root . '.php';
$data = ob_get_contents();
ob_end_clean();
}
public function location()
{
//use func_get_args(), to compile a new Route! and return with BASE_HREF!
}
}
Then you have a set of functions and helpers all locked in one class for the template system.