Home > Backend Development > PHP7 > How does php7 learn from other frameworks and write its own framework?

How does php7 learn from other frameworks and write its own framework?

醉折花枝作酒筹
Release: 2023-02-18 07:38:01
forward
2048 people have browsed it

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.

How does php7 learn from other frameworks and write its own framework?

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.";
});
Copy after login
// 第二种,使用url分发到控制器
App::router()->post('/user/info/[0-9]+(/[0-9]+)?', 'App\Controllers\UserController@getUserInfo');
Copy after login

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());
            };
        }
Copy after login

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;
    }
Copy after login

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
Copy after login

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");
Copy after login

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template