Home > Article > Backend Development > PHP MVC framework core classes
PHP MVC Framework Core Class
Now let’s give some examples of core frameworks: Create a Framework.class.php file under framework/core. Write the following code:
// framework/core/Framework.class.php
class Framework {
public static function run() {
echo "run()";
}
require "framework/core/Framework.class.php";
Framework::run();
You can visit index.php in your browser to see the result. Usually this static method is named run() or bootstrap(). In this method, we have to do three main things:
class Framework {
public static function run() {
// echo "run()";
self::init();
self:: autoload();
self::dispatch();
}
private static function init() {
}
private static function autoload() {
}
private static function dispatch() {
}
}
initialization
init () method:
// Initialization
private static function init() {
// Define path constants
define("DS", DIRECTORY_SEPARATOR);
define("ROOT", getcwd() . DS) ;
define("APP_PATH", ROOT . 'application' . DS);
define("FRAMEWORK_PATH", ROOT . "framework" . DS);
define("PUBLIC_PATH", ROOT . "public" . DS );
define("CONFIG_PATH", APP_PATH . "config" . DS);
define("CONTROLLER_PATH", APP_PATH . "controllers" . DS);
define("MODEL_PATH", APP_PATH . "models" . DS);
define("VIEW_PATH", APP_PATH . "views" . DS);
define("CORE_PATH", FRAMEWORK_PATH . "core" . DS);
define('DB_PATH', FRAMEWORK_PATH . "database" . DS);
define("LIB_PATH", FRAMEWORK_PATH . "libraries" . DS);
define("HELPER_PATH", FRAMEWORK_PATH . "helpers" . DS);
define("UPLOAD_PATH", PUBLIC_PATH . loads " . DS);
// Define platform, controller, action, for example:
// index.php?p=admin&c=Goods&a=add
define("PLATFORM", isset($_REQUEST['p' ]) ? $_REQUEST['p'] : 'home');
define("CONTROLLER", isset($_REQUEST['c']) ? $_REQUEST['c'] : 'Index');
define("ACTION", isset($_REQUEST['a']) ? $_REQUEST['a'] : 'index');
define("CURR_CONTROLLER_PATH", CONTROLLER_PATH . PLATFORM . DS);
define(" CURR_VIEW_PATH", VIEW_PATH . PLATFORM . DS);
// Load core classes
require CORE_PATH . "Controller.class.php";
require CORE_PATH . "Loader.class.php";
require DB_PA TH . "Mysql .class.php";
require CORE_PATH . "Model.class.php";
// Load configuration file
$GLOBALS['config'] = include CONFIG_PATH . "config.php";
// Start session
session_start();
}
You can see the purpose of each step in the comments.
Automatic loading
In the project, we don’t want to manually include or require loading when we want to use a class in the script. This is why the PHP MVC framework has an automatic loading function. For example, in symfony, if you want to load a class under lib, it will be automatically imported. Amazing, right? Now let's add autoloading functionality to our framework.
Here we are going to use the built-in function spl_autoload_register in PHP:
// Autoloading
private static function autoload(){
spl_autoload_register(array(__CLASS__,'load'));
}
// Define a custom load method
private static function load($classname){
// Here simply autoload app's controller and model classes
if (substr($classname, -10) == "Controller"){
// Controller
Require_once Curr_Controller_Path. "$ ClassName.class.php";
} Elseif (Substr ($ className, -5) == "Model") Re_ONCE MODEL_PATH. "$ ClassName .class.php";
}
}
Every framework has its own naming rules, and ours is no exception. For a controller class, it needs to be named something like xxxController.class.php, and for a model class, it needs to be named xxModel.class.php. Why do you need to follow the naming rules of a framework when using it? Automatic loading is one reason.
Routing/Distribution
// Routing and dispatching
private static function dispatch(){
// Instantiate the controller class and call its action method
$controller_name = CONTROLLER . "Controller";
$action_name = ACTION . "Action";
$controller = new $controller_name;
$controller->$action_name();
}
In this step, index.php will distribute the request to the corresponding Controller::Aciton( ) method.
Basic Controller Class
Usually there is a basic controller in the core class of the framework. In symfony, it's called sfAction; in iOS, it's called UIViewController. Here we name it Controller and create Controller.class.php under framework/core
// Base Controller
class Controller{
// Base Controller has a property called $loader, it is an instance of Loader class(introduced later)
protected $loader;
public function __construct(){
$this->loader = new Loader();
}
public function redirect($url,$message,$wait = 0){
if ($ wait == 0) {
header ("local: $ url");
} else {
include curr_view_path. "Message.html"; $loader, which is an instantiation of the Loader class (described later). To be precise, $this->loader is a variable pointing to the instantiated Load class. I won't discuss it too much here, but this is indeed a very key concept. I've met some PHP developers who believe that after this statement:
$this->loader = new Loader();
$this->load is an object. No, it's just a reference. This has been used since Java, and before Java it was called pointers in C++ and Objective C. A reference is an encapsulated pointer type. For example, in iOS (O-C), we create an object:
UIButton *btn = [UIButton alloc] init];
Load class
In framework.class.php, we have encapsulated the application’s controller and model of automatic loading. But how to automatically load classes in the framework directory? Now we can create a new Loader class, which will load the classes and functions in the framework directory. When we load the framework class, we only need to call the method in this Loader class.
class Loader{
// Load library classes
public function library($lib){
include LIB_PATH . "$lib.class. using using using using using using using using using using out out out out out out out out out out out of out. function helper($helper){
include HELPER_PATH . "{$helper}_helper.php";