PHP builds its own MVC framework from scratch

Time:2019-9-10

This paper illustrates how PHP builds its own MVC framework entry file from scratch. Share for your reference, as follows:

First, let’s look at the running process of the framework.

Entry File – > Definition Constant – > Introduction Library – > Automatic Loading Class – > Startup Framework – > Route Resolution – > Loading Controller – > Return Results

Entry file index.php:

<?php
/*
Entry file
1. Define constants
2. Loading function library
3. Launching Framework
*/
// Define the root directory of the current framework
define('IMOOC', __DIR__);
// Define the directory where the framework core file is located
define('CORE', IMOOC.'/core');
// The directory where the project files are located
define('APP', IMOOC.'/app');
// Define project debugging patterns
define('DEBUG', true);
// Determine whether the project is in debugging state
if (DEBUG) {
  // Set the error level: Display all errors
  ini_set('display_error', 'On');
}else{
  ini_set('display_error','Off');
}
// Loading function library
include CORE.'/common/function.php';
// Loading Framework Core Files
include CORE.'/imooc.php';
\core\Imooc::run();

Common function. PHP in the framework core directory:

<?php
/*
Output corresponding variables or arrays
*/
function p($var){
  if(is_bool($var)){
    var_dump($var);
  }elseif (is_null($var)) {
    var_dump(NULL);
  }else{
    echo '<pre style="position:relative;z-index:1000;padding:10px;border-radius:5px;background:#f5f5f5;border:1px solid #aaa;font-size:14px;line-height:18px;opacity:0.9;">'.print_r($var,true).'</pre>';
  }
}

Framework core file imooc.php:


<?php
namespace core;
class Imooc
{
  static public function run()
  {
    p('ok');
  }
}

Run the project, access the entry file index. php, browser output one as scheduled:

More readers interested in PHP-related content can see the topics of this site: Introduction to Php Object-Oriented Programming, Introduction to PHP Array Operating Skills, Introduction to PHP Basic Grammar, Summary of PHP Operating and Operator Usage, Summary of Php String Usage, and php+mysql Data. Introduction to Library Operations and Summary of Common Database Operating Skills in PHP

I hope this article will be helpful to everyone’s PHP program design.