The core link of the framework is from the initial request routing resolution to the distribution of the controller, and the data interaction of the model to the response. Implementation using other frameworks would be very cumbersome and integrate too much content, many of which are not needed, so I wrote a simple and practical framework based on other frameworks.
Let’s start with routing:
1) Routing
The rules of the routing protocol use regular expression matching. Different links are distributed to different controllers, and it also supports calling anonymous functions
// 第一种,使用匿名函数 App::router()->get('/', function () { echo "Beauty Framework."; });
// 第二种,使用url分发到控制器 App::router()->post('/user/info/[0-9]+(/[0-9]+)?', 'App\Controllers\UserController@getUserInfo');
The core code implemented: (is it very simple?)
if (is_string($lostrcallable) && preg_match('!^([\a-zA-Z0-9]+)\@([a-zA-Z0-9]+)$!', $lostrcallable, $matches)) { $class = $matches[1]; $method = $matches[2]; $callable = function () use ($class, $method) { static $obj = null; if ($obj === null) { $obj = new $class; } return call_user_func_array(array($obj, $method), func_get_args()); }; }
2) Controller
No, wherever you point, you can process the request. However, according to MVC, it is recommended to write it in the Controller. Everyone thinks so, so just do it;
3) Model layer
The Dao class is directly integrated here for database query operations. Because many people are now using MySQL, the regular operations of MySQL are coupled and implemented. Because the native operations of the database will cause a lot of injection and other problems, there is no Supports direct Qquery method.
4) DAO layer
Because in many cases it is necessary to separate reading and writing, but it is too cumbersome to use MySQL Proxy to achieve it, so it has implemented support for database query according to different sql Use master and slave databases respectively.
How to do it?
It is directly divided according to unused query operations. Select query operations are directly assigned to the reading database, and Insert, Update, and Delete are directly assigned to the writing database. You can also use onMasterConnection/onSlaveConnection to specify directly.
Look at how Select is done. This part is the implementation logic:
/** * 获取查询内容,以数组形式返回,数据内为array。 * * @access public * @param integer|array $limit Array to define SQL limit in format Array ($count, $offset) * or only $count * * @return mixed Array of Clients */ protected function get($limit = null) { $this->dbClient->setQueryChannel($this->channel); $results = $this->dbClient->arrayBuilder()->get($this->dbTable, $limit, $this->fields); $this->_reset(); if ($this->dbClient->count == 0) { return []; } return $results; }
5) Configuration class
In the development environment and The production environment will use different configuration environments, and .env environments can be created in the framework to switch. Development and production can be set routinely in config. Which environment is used is set in .env
; This is a environment configuration file environment=development
Of course, there is also some public configuration information, which can be set directly in the config file, so that it can be used in any environment. use.
// 支持使用.连接访问配置项,访问的是config/app.php中的config key \Beauty\Core\App::config()->get("app.config");
6) Request response
Because there is no need for View and only json data needs to be returned, text/json is written directly in the Header. = =
Other implementations
memcache and redis use consistent hashing to access different instances.
validator verification class
The specific codes are on github https://github.com/tigerk/Beauty
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of How does php7 learn from other frameworks and write its own framework?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool