Build your own PHP framework-abstract framework content, php framework-Abstract
In the previous blog, we set up a simple framework from the public/index of a single portal. php enters, parses the corresponding Controller and Action, executes it, renders the corresponding page or outputs the corresponding data.
But we can see in public/index. in the PHP file, there is a bunch of code. If I want to add permissions, events, and other mechanisms later, it is not hard to imagine, not in the future, index. php will become a huge php file. We certainly don't want to see such a situation, so we need to abstract the content to keep the entry file simple and clear.
So where should we abstract this content? Some smart people have already thought of the position defined in composer. json. Let's take a look at the composer. json file:
{ "name": "craryprimitiveman/simple-framework", "description": "A simple php framework", "license": "MIT", "authors": [ { "name": "harrysun", "email": "sunguangjun@126.com" } ], "require": {}, "autoload": { "psr-4": { "sf\\": "src/", "app\\": "" } }, "repositories": [ {"type": "composer", "url": "http://packagist.phpcomposer.com"}, {"packagist": false} ]}
We can see that there is a sf \ In the psr-4 in autoload, his address is src/, this is where we want to put abstract content.
Someone may ask, why not use app \ as the namespace directly? After thinking about it, we need to make the content under src into a composer package and migrate it to the vendor.
The following is a formal abstraction.
Create two folders under src, one is base and the other is web. Base is used to store basic classes, and web is used to store web-related classes. In the future, the framework may support php script execution, so we need to separate the base from the web. To add a php script in the future, we only need to create a console folder.
Create the Application. php file in the two folders.
Let's take a look at Application. php In the base.
<?phpnamespace sf\base;use Exception;/** * Application is the base class for all application classes. * @author Harry Sun <sunguangjun@126.com> */abstract class Application{ /** * @var string the namespace that controller classes are located in. * This namespace will be used to load controller classes by prepending it to the controller class name. * The default namespace is `app\controllers`. */ public $controllerNamespace = 'app\\controllers'; /** * Runs the application. * This is the main entrance of an application. */ public function run() { try { return $this->handleRequest(); } catch (Exception $e) { return $e; } } /** * Handles the specified request. */ abstract public function handleRequest();}
It is an abstract class that implements a simple run method. The run method is to execute the following handleRequest method.
It defines an abstract method handleRequest, waiting to be inherited and implemented.
It defines the controllerNamespace attribute and records the namesapce stored in the controller. The default value is 'app \ controllers '.
Let's look at Application. php In the web.
<?phpnamespace sf\web;/** * Application is the base class for all application classes. * @author Harry Sun <sunguangjun@126.com> */class Application extends \sf\base\Application{ /** * Handles the specified request. * @return Response the resulting response */ public function handleRequest() { $router = $_GET['r']; list($controllerName, $actionName) = explode('/', $router); $ucController = ucfirst($controllerName); $controllerName = $this->controllerNamespace . '\\' . $ucController . 'Controller'; $controller = new $controllerName(); return call_user_func([$controller, 'action'. ucfirst($actionName)]); }}
Is it very familiar? In fact, it is to put the content previously put in index. php into the handleRequest method of Application.
Then we need to call the code from the entry file, which is very simple. The content of index. php is as follows:
<?phprequire_once(__DIR__ . '/../vendor/autoload.php');$application = new sf\web\Application();$application->run();
Directly go to the new web Application instance and execute the run method. Isn't it easy.
Visit: http: // localhost/simple-framework/public/index. php? R = site/test. You can see the same result last time.
Now, we will be here today. The project content and Blog content will also be put on Github. You are welcome to give suggestions.
Code: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.2
Blog project: https://github.com/CraryPrimitiveMan/create-your-own-php-framework