PHP lightweight architecture and coding conventions



Architecture principle:
everything is an object, abandoning functions and global variables ( procedure-oriented and function-oriented have their own specific advantages, while using JAVA language can write high-quality or poor process-oriented and function-oriented code, php is here only for implementation automatic class file loading ).
Everything has namespaces.

Specific implementation:
ROOT is the site root directory

Code snippet
#ROOT/.htaccess
#Forward requests from the site root directory to the project root directory
RewriteRule .* /com/kb/$0 [L]


#ROOT/com/kb/.htaccess
#Agreed that ROOT/com/kb/app is the directory where the php file is located, and all php requests are forwarded to ROOT/com/kb/index.php
#There are no php files in the rest of the directories
RewriteRule ^app.*\.php$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]


All php files have the following conventions:
Except for the view module php and ROOT/com/kb/index.php, each of the other php files corresponds to a class, and the namespace, class name and file path should be consistent, such as ROOT/com/kb/app /Xxx.php namespace is \com\kb\app

//ROOT/com/kb/index.php code snippet
//One of the ways to implement class autoloading
namespace {

    function __autoload($classname) {
        $class_path = \str_replace('\\', DIRECTORY_SEPARATOR, $classname);
        $file = __DIR__ . '/../../' . $class_path . '.php';
        if (\file_exists($file)) {
            require_once($file);
            if (\class_exists($classname, false)) {
                return true;
            }
        }
        return false;
    }

}


On this basis, the complete MVC pattern is realized.

A collection of commonly used tool classes under ROOT/com/fall


Related reading :
My java web architecture scheme http://afadgaeg.iteye.com/blog/2395132

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326294291&siteId=291194637