來!狂擼一款PHP現代化框架 (準備工做)

clipboard.png

前言

從本章開始,咱們繼續造輪子,去完成一款相似於Laravel的現代化PHP框架,爲何說是現代化?由於他必須具有一下幾點php

  • 遵照PSR-4編碼規範
  • 使用Composer進行包管理
  • 標準的HTTP請求方式
  • 優雅的使用設計模式

開始咱們無需關心性能問題,先考慮框架具體須要實現哪些功能,這與實現業務就大不相同了,來!開始個人表演。html

前期

作任何一件事情都要有個前期準備工做。git

  1. 做爲PSR-4的規定,咱們命名空間得有一個祖宗名字,這裏我叫他神聖的 《z_framework》
  2. 至少須要一個GITHUB庫來存儲這個項目 https://github.com/CrazyCodes...
  3. 建立一個composer.json文件用於進行包管理,灰常簡單,phpunit搞進來。經過psr-4加載個項目命名github

    {
      "name": "z framework",
      "require-dev": {
        "phpunit/phpunit": "^7.0"
      },
      "autoload": {
        "psr-4": {
          "Zero\\": "src/Zero",
        }
      },
      "autoload-dev": {
        "psr-4": {
          "Zero\\Tests\\": "tests/"
        }
      }
    }

最後咱們就須要考慮下目錄的結構及其咱們第一步要完成的功能,核心的結構(這裏並不是只的項目結構哦。是框架的核心結構)暫且是這樣json

  • src設計模式

    • Zerophp7

      • Config // 可能存放一些配置文件的解析器
      • Container // 容器的解析器
      • Http // 請求處理的一些工具
      • Routes // 路由處理的一些功能
      • Bootstrap.php // 這多是一個啓動腳本
      • Zero.php // 多是核心的入口文件
  • tests // 測試目錄
  • .gitignore
  • composer.json
  • LICENSE
  • README.md

路由

還記得第一次使用Laravel時咱們第一步作的事情嗎?是的,去研究路由,因此咱們把路由做爲框架的第一步。在研究路由前,咱們要知道app

http://www.domain.com/user/create

是如何實現的,php默認是必須請求index.php或者default.php的,上述連接實際隱藏了index.php或default.php ,這是Nginx等服務代理幫咱們作到的優雅的連接,具體配置以下,實際與Laravel官方提供無差異composer

server {
  listen       80;
  server_name  www.zf.com;
  root          /mnt/app/z_framework/server/public;
  index         index.php index.html index.htm;

  location / {
     try_files $uri $uri/ /index.php?$query_string;
   }

  location ~ \.php$ {
     fastcgi_pass   php71:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include        fastcgi_params;
  }
}

經過框架

try_files $uri $uri/ /index.php?$query_string;

去解析請求,經過上述能夠得出

http://www.domain.com/user/create
=======
http://www.domain.com/index.php?user/create

好了,明白了其中奧祕後,咱們開始路由的編寫,在src/Routes/Route.php

namespace Zero\Routes;
    
class Route 
{
}

實現

首先咱們先建立一個簡單的接口文件
src/Routes/RouteInterface.php

namespace Zero\Routes;
    
interface RouteInterface
{
    public function Get($url, $callFile);
    
    public function Post($url, $callFile);
    
    public function Put($url, $callFile);
    
    public function Delete($url, $callFile);
}

從Get請求開始

namespace Zero\Routes;
    
class Route implements RouteInterface
{
    public function Get($url, $callFile)
    {
    
    }
}

最後實現Get代碼塊

if (parent::isRequestMethod("GET")) { // 判讀請求方式
                
    if (is_callable($callFile)) { // 判斷是不是匿名函數
        return $callFile();
    }
    
    if ($breakUpString = parent::breakUpString($callFile)) { // 獲取Get解析。既/user/create 
        header('HTTP/1.1 404 Not Found');
    }
    
    try {
        // 經過反射類獲取對象 $breakUpString[0] = user
        $reflectionClass = new \ReflectionClass('App\\Controllers\\' . $breakUpString[0]);
        // 實例化對象
        $newInstance     = $reflectionClass->newInstance();
        // 獲取對象中的指定方法,$breakUpString[1] = create
        call_user_func([
            $newInstance,
            $breakUpString[1],
        ], []);
    } catch (\ReflectionException $e) {
        header('HTTP/1.1 404 Not Found');
    }
} else {
    header('HTTP/1.1 404 Not Found');
}

return "";

若是你想測試上述代碼,可以使用phpunit,或者傻大粗的方式,這裏便於理解使用傻大粗的方式

clipboard.png

建立一個目錄,隨後按照Laravel的目錄形式建立幾個目錄,

<?php
    
namespace App\Controllers;

class UserController
{
    public function create()
    {
       var_dump(0);
    }
}

最後public/index.php文件中去調用路由

require_once "../../vendor/autoload.php";

Zero\Zero::Get("user", "UserController@create");

到這裏咱們就基本完成了路由的功能,下一章將完善路由的編碼

致謝

感謝你看到這裏,但願本篇能夠幫到你。具體代碼在 https://github.com/CrazyCodes...

相關文章
相關標籤/搜索