Home  >  Article  >  Backend Development  >  Beauty Framework implemented based on php7 (with code)

Beauty Framework implemented based on php7 (with code)

不言
不言Original
2018-08-01 13:39:191842browse

This article introduces you to the Beauty Framework framework (with code) based on php7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you

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.
Using other frameworks to implement it will be very cumbersome, and there are too many integrated contents, 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 to distribute different links to different controllers Up, 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 in it: (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
If you don’t have it, you Wherever it points, the request can be processed, but 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 support for direct Qquery. way to do it.

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 based on Different sql uses 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 does it. 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 you can create an .env environment 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

Recommended related articles:

Zend Framework Smarty extension implementation method, zendsmarty

Zend Framework routing mechanism code analysis, zendframework

The above is the detailed content of Beauty Framework implemented based on php7 (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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