开发基本的php框架

Posted 郑楚周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发基本的php框架相关的知识,希望对你有一定的参考价值。

github路径:https://github.com/zhengchuzhou/easyphpFramework

一、目录结构及用途

 

二、相关代码:

1、入口文件(index.php):

<?php
/**
 * 入口文件
 * 1、定义常量
 * 2、加载函数库
 * 3、启动框架
 */
define(\'ROOT\', realpath(\'./\'));
define(\'CORE\', ROOT.\'/core\');
define(\'APP\', ROOT.\'/app\');
define(\'MODULE\', \'app\');

include "vendor/autoload.php"; // 引入composer加载的类

define(\'DEBUG\', true);
if (DEBUG) {
    $whoops = new \\Whoops\\Run;
    $whoops->pushHandler(new \\Whoops\\Handler\\PrettyPageHandler);
    $whoops->register();
    ini_set(\'display_errors\', \'On\');
} else
    ini_set(\'display_errors\', \'Off\');

include_once CORE.\'/common/function.php\';
include_once CORE.\'/core.php\';

spl_autoload_register(\'\\core\\core::load\');

\\core\\core::run();

2、公用函数库(function.php):

<?php
/**
 * 公告函数库
 */

function p($var)
{
    if (is_bool($var))
        var_dump($var);
    elseif (is_null($var))
        var_dump(NULL);
    else {
        echo "<pre style=\'position:relative; z-inenx: 1000; padding: 10px; border-radius: 5px; background: #F5F5F5; 
        border: 1px solid #aaa; font-size: 14px; line-height: 18px; opacity: 0.9;\'>" . print_r($var, true) . "</pre>";
    }
}

 3、核心文件(core.php):

<?php
/**
 * 核心文件
 */
namespace core;

use core\\lib\\log;

class core
{
    public $assign = [];
    static public function run()
    {
        log::init();
        $route = new \\core\\lib\\route();

        $controller = $route->controller;
        $action = $route->action;
        $file = APP.\'/controller/\'.$controller.\'Controller.php\';
        $class = \'\\\\\'.MODULE.\'\\\\controller\\\\\'.$controller.\'Controller\';
        if (is_file($file)) {
            include_once $file;
            $class = new $class();
            $class->$action();
        } else
            throw new \\Exception(\'找不到控制器:\'.$controller);
    }

    static public function load($class)
    {
        //  自动加载类库
        $class = str_replace(\'\\\\\', \'/\', $class);
        $file = ROOT.\'/\'.$class.\'.php\';
        if (is_file($file))
            include_once $file;
        else
            return false;
    }

    public function assign($name, $value)
    {
        $this->assign[$name] = $value;
    }

    public function display($file)
    {
        $file = APP.\'/view/\'.$file.\'.php\';
        if (is_file($file)) {
            extract($this->assign);
            include $file;
        } else
            throw new \\Exception(\'找不到该视图文件\');
    }
}

4、路由文件(route.php):

<?php
/**
 * 路由文件
 */
namespace core\\lib;
use \\core\\lib\\conf;

class route
{
    public $controller;
    public $action;
    public function __construct()
    {
        $uri = $_SERVER[\'REQUEST_URI\'];
        if (isset($uri) && $uri != \'/\') {
            $uriArr = explode(\'/\', trim($uri, \'/\'));
            if (isset($uriArr[0])) {
                $this->controller = $uriArr[0];
                unset($uriArr[0]);
            }
            if (isset($uriArr[1])) {
                $this->action = $uriArr[1];
                unset($uriArr[1]);
            }
            else
                $this->action = conf::getConf(\'ACTION\', \'route\');
            //  uri多余部分转化成GET
            for ($i = 2; $i < count($uriArr) + 2; $i += 2)
                isset($uriArr[$i + 1]) ? $_GET[$uriArr[$i]] = $uriArr[$i + 1] : \'\';
        } else {
            $this->controller = conf::getConf(\'CONTROLLER\', \'route\');
            $this->action = conf::getConf(\'ACTION\', \'route\');
        }
        log::log(\'controller:\'.$this->controller.\', action:\'.$this->action.\', get:\'. json_encode($_GET));
    }
}

5、数据库模型类(model.php):

<?php
/**
 * model类
 */
namespace core\\lib;
use \\core\\lib\\conf;

class model extends \\PDO
{
    public function __construct()
    {
        try {
            $conf = conf::getAll(\'db\');
            parent::__construct($conf[\'DSN\'], $conf[\'USERNAME\'], $conf[\'PASSWD\']);
        } catch (\\PDOException $e) {
            p($e->getMessage());
        }
    }
}

6、配置文件类(conf.php):

<?php
/**
 * 配置类
 */
namespace core\\lib;

class conf
{
    static public $conf = [];
    static public function getConf($name, $file)
    {
        /**
         * 1、判断文件是否存在
         * 2、判断配置是否存在
         * 3、缓存配置
         */
        $conf = self::getAll($file);
        if (isset($conf[$name]))
            return $conf[$name];
        else
            throw new \\Exception(\'没有这个配置项\'.$name);
    }

    static public function getAll($file)
    {
        /**
         * 1、判断文件是否存在
         * 2、缓存配置
         */
        if (!isset(self::$conf[$file])) {
            $path = ROOT.\'/core/config/\'.$file.\'.php\';
            if (!is_file($path)) {
                throw new \\Exception(\'找不到配置文件:\'.$file);
                die();
            } else {
                self::$conf[$file] = include $path;
            }
        }
        return self::$conf[$file];
    }
}

7、日志类(log.php):

<?php
/**
 * 日志类
 */
namespace core\\lib;
use \\core\\lib\\conf;

class log
{
    static public $class;
    /**
     * 1、确定日志存储方式
     * 2、写日志
     */
    static public function init()
    {
        //  确定存储方式
        $drive = conf::getConf(\'DRIVE\', \'log\');
        $class = \'\\core\\lib\\log\\\\\'.$drive;
        self::$class = new $class;
    }

    static public function log($message, $file = \'log\')
    {
        self::$class->log($message, $file);
    }
}

8、文件日志驱动类(file.php):

<?php
namespace core\\lib\\log;

use core\\lib\\conf;

class file
{
    public $path;
    public function __construct()
    {
        $this->path = conf::getConf(\'OPTION\', \'log\')[\'PATH\'].date(\'Ymd\').\'/\';
    }

    public function log($message, $file = \'log\')
    {
        /**
         * 1、确定日志存储目录是否存在
         *  不存在,新建目录
         * 2、写入日志
         */
        if (!is_dir($this->path))
            mkdir($this->path, \'0777\', \'true\');
        $message = date(\'Y-m-d H:i:s\') . \' \' . json_encode($message) . PHP_EOL;
        file_put_contents($this->path.$file.\'.php\', $message, FILE_APPEND);
    }
}

9、相关的配置文件:

数据库配置文件(db.php):

<?php
/**
 * 数据库配置文件
 */
return array(
    \'DSN\' => \'mysql:host=localhost; dbname=test\',
    \'USERNAME\' => \'root\',
    \'PASSWD\' => \'xy123456\'
);

日志配置文件(log.php):

<?php
/**
 * 日志配置文件
 */
return array(
    \'DRIVE\' => \'file\',
    \'OPTION\' => [
        \'PATH\' => ROOT.\'/log/\'
    ]
);

路由配置文件(route.php):

<?php
return array(
    \'CONTROLLER\' => \'index\',
    \'ACTION\' => \'index\'
);

 三、nginx配置,优化url路径

if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php/$1 last;
}

四、配置composer,加载php扩展

{
    "name": "TEST PHP",
    "description": "PHP Framework",
    "type": "Framework",
    "keywords": [
        "PHP", "PHP Framework"
    ],
    "require": {
        "php": ">= 5.3.0",
        "filp/whoops": "*",
        "symfony/var-dumper": "*",
        "catfan/medoo": "*"
    }
}

 

以上是关于开发基本的php框架的主要内容,如果未能解决你的问题,请参考以下文章

PHP必用代码片段

超级有用的9个PHP代码片段

超实用的php代码片段

php基本框架目录

php基本框架目录

56个PHP开发常用代码