All Projects → leizongmin → leiphp

leizongmin / leiphp

Licence: other
轻量级的 PHP MVC 框架 Lightweight MVC framework for simplistic PHP apps

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to leiphp

Denunciado
This project born from the need from people to have a way of communication between municipalities and communities. Some municipalities, have their platforms, but they are complex to validate the veracity of complaints. Denounced, it was born with the purpose of offering a free platform to these municipalities. Denounced consists of three main modules developed with Microsoft technologies, using the .Net Framework and Xamarin for its development: 1. Back End Web Project: Module of administration of the complaints, by the employees of the town councils. In this tool, the employees of the city council receive, validate, report and close the complaints, after being served. 2. Web Portal Client: It consists of a web project, so that the community make their complaints, in the same, the users of the service create a profile, must specify when making their complaint, evidence to support this. Through the portal, they can see the complaints of other community members, follow it, give their opinion or provide possible solutions or more evidence. 3. Mobile Project: It has the same functionalities as the web portal, with the addition, that the automatic location can be sent, from the cell phone.
Stars: ✭ 183 (+510%)
Mutual labels:  mvc
Javacollection
Java开源项目之「自学编程之路」:学习指南+面试指南+资源分享+技术文章
Stars: ✭ 2,957 (+9756.67%)
Mutual labels:  mvc
Respo
A virtual DOM library built with ClojureScript, inspired by React and Reagent.
Stars: ✭ 230 (+666.67%)
Mutual labels:  mvc
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (+526.67%)
Mutual labels:  mvc
Acgn Community
A community app for news,animation,music and novels developed material design style.
Stars: ✭ 193 (+543.33%)
Mutual labels:  mvc
Application
🏆 A full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.
Stars: ✭ 205 (+583.33%)
Mutual labels:  mvc
Aspnetcorelocalization
Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
Stars: ✭ 183 (+510%)
Mutual labels:  mvc
Ssh
ssh员工管理系统
Stars: ✭ 252 (+740%)
Mutual labels:  mvc
Mojo
✨ Mojolicious - Perl real-time web framework
Stars: ✭ 2,298 (+7560%)
Mutual labels:  mvc
Quiz App
A repository reflecting the progress made on the "How to Build iOS Apps with Swift, TDD & Clean Architecture" YouTube series, by Caio & Mike.
Stars: ✭ 230 (+666.67%)
Mutual labels:  mvc
Cfwheels
An open source ColdFusion framework inspired by Ruby on Rails.
Stars: ✭ 188 (+526.67%)
Mutual labels:  mvc
Utron
A lightweight MVC framework for Go(Golang)
Stars: ✭ 2,205 (+7250%)
Mutual labels:  mvc
Coldbox Platform
A modern, fluent and conventions based HMVC framework for ColdFusion (CFML)
Stars: ✭ 220 (+633.33%)
Mutual labels:  mvc
Smartstorenet
Open Source ASP.NET MVC Enterprise eCommerce Shopping Cart Solution
Stars: ✭ 2,363 (+7776.67%)
Mutual labels:  mvc
Opentouryo
”Open棟梁”は、長年の.NETアプリケーション開発実績にて蓄積したノウハウに基づき開発した.NET用アプリケーション フレームワークです。 (”OpenTouryo” , is an application framework for .NET which was developed using the accumulated know-how with a long track record in .NET application development.)
Stars: ✭ 233 (+676.67%)
Mutual labels:  mvc
Thinkgo
A lightweight MVC framework written in Go (Golang).
Stars: ✭ 184 (+513.33%)
Mutual labels:  mvc
Circleoffriendsdisplay
朋友圈的做法
Stars: ✭ 205 (+583.33%)
Mutual labels:  mvc
openimmo
OpenImmo library
Stars: ✭ 36 (+20%)
Mutual labels:  composer-package
Leaf
🍁 The easiest way to create clean, simple but powerful web apps and APIs quickly
Stars: ✭ 248 (+726.67%)
Mutual labels:  mvc
Flexml
🚀基于Litho的Android高性能动态业务容器。
Stars: ✭ 225 (+650%)
Mutual labels:  mvc

version php license downloads

LeiPHP 轻量级的 PHP MVC 框架

Lightweight MVC framework for simplistic PHP apps.

LeiPHP is contained in one single PHP file. It works based on a config file and is perfect for small to medium PHP-powered projects.

Features:

  • Supports database interaction
  • Support for file uploads
  • Debugging tools
  • File dependency management
  • Template engine
  • REST-based routing
  • Easy to initialize

此框架仅有一个文件,其中包含了MySQL数据库、上传文件、调试信息、导入依赖文件、模板和REST路由等一系列常用操作。API接口简单,学习成本低,开箱即用,适合用来快速写一些对性能要求不高的程序。

项目文件结构

.
├── action          路由处理程序目录
├── global.inc.php  项目公共文件文件
├── index.php       项目入口文件
├── lib             公共代码目录
├── public          静态资源文件目录
└── template        模板目录

安装

  • 直接下载lei.php文件:wget https://raw.githubusercontent.com/leizongmin/leiphp/master/lei.php
  • 通过composer安装:composer require leizongmin/leiphp

初始化

首先新建一个global.inc.php文件,所有程序通过加载该文件来进行配置及初始化:

<?php
/**
 * 公共文件
 */

// 载入LeiPHP
require('lei.php');

// 当前应用的根目录
APP::set('ROOT', dirname(__FILE__ ).'/');
// 模板根目录
APP::set('TEMPLATE_ROOT', APP::get('ROOT').'template/');

// 输出调试信息,生成环境请去掉这行或设置为false
APP::set('DEBUG', true);

// MYSQL数据库配置,如果不定义数据库配置,则不自动连接数据库
APP::set('MYSQL_SERVER', 'localhost:3306');  // 服务器,默认为 localhost:3306,使用长连接在地址前加 p:,如:p:localhost:3306
APP::set('MYSQL_USER',   'root');            // 用户名,默认为 root
APP::set('MYSQL_PASSWD', '123456');          // 密码,默认为空
APP::set('MYSQL_DBNAME', 'test');            // 数据库名,默认为空
APP::set('MYSQL_PERMANENT', false);          // 使用使用永久连接,默认false

// 初始化
APP::init();
?>

如果通过composer安装,则载入路径应改为:

require('vendor/leizongmin/leiphp/lei.php');

在所有php程序中,均可载入global.inc.php文件唉实现初始化LeiPHP:

<?php
require('global.inc.php');
// ...
?>

REST路由

LeiPHP可以根据不同的请求方法来调用相应的处理函数完成请求,比如:

<?php
require('global.inc.php');

// 这里是公共部分的代码,所有请求方法都会执行下面的代码
echo '所有请求方法都会执行这里的代码';

// 定义处理GET请求的代码
function method_get () {
  echo 'GET请求方法的处理代码';
}

// 定义处理POST请求的代码
function method_post () {
  echo 'POST请求方法的处理代码';
}

// 定义处理DELETE请求的代码
function method_delete () {
  echo 'DELETE请求方法的处理代码';
}

// 定义处理PUT请求的代码
function method_put () {
  echo 'PUT请求方法的处理代码';
}

?>

模板渲染

LeiPHP中提供了一个静态类 TPL 来渲染HTML模板:

// 设置模板变量
TPL::set_val('模板变量', '值');
// 渲染模板
TPL::render('模板名');

模板文件存放在template目录内,比如要渲染template/index.html

TPL::render('index');

模板文件中通过$locals变量来获取模板数据:

<?= $locals['模板变量'] ?>
<?php foreach ($item as $list): ?>
  <?= $item ?>
<?php endforeach; ?>

以下为模板渲染相关的方法:

  • TPL::get($name, $locals) 载入模板文件,若不指定后缀名,会自动加上.html,以常量APP_TPL_ROOT定义的模板目录作为根目录,模板文件实际上为php程序文件,第二个参数为模板中可用的变量,在模板中通过$locals来读取(若无命名冲突也可以直接使用键名),返回渲染后的内容;
  • TPL::set_val($name, $value) 设置模板变量;
  • TPL::get_val($name) 取模板变量值;
  • TPL::render($name, $locals, $layout = '') 自动为$locals加上用APP::set_val()设置的变量,并渲染模板。如果指定了视图模板$layout,则需要在视图模板中通过$body变量来获取模板内容;

操作MySQL数据库

LeiPHP中提供了一个静态类 SQL 来操作MySQL数据库(基于 mysqli 实现):

  • SQL::connect($server = 'localhost:3306', $username = 'root', $password = '',$database = '');连接到数据库,当配置了数据库连接时,leiapp会自动执行此方法来连接到数据库,若你的程序中已经通过mysqli_connect来创建了一个数据库连接,可以不用再执行此方法连接数据库(如果要使用永久连接来提高性能,可以在$server前加字符串p:,如:p:localhost:3306);
  • SQL::find_all($sql) 查询SQL,并返回数组格式的结果,失败返回FALSE
  • SQL::find_one($sql) 查询SQL,仅返回第一条结果,失败返回FALSE
  • SQL::update($sql) 查询SQL,返回受影响的记录数,一般用于执行插入或更新操作;
  • SQL::id()SQL::lastId() 返回最后插入的一条记录的ID;
  • SQL::errno() 返回最后执行的一条SQL语句的出错号;
  • SQL::errmsg() 返回最后执行的一条SQL语句的出错信息;
  • SQL::escape($str) 返回安全的SQL字符串;

更简便的数据库操作:

  • SQL::find_all($table, $where) 查询所有记录,其中$table是表名,$where是一个条件数组,如:array('id' => 1)
  • SQL::find_one($table, $where) 查询一条记录;
  • SQL::update($table, $where, $update) 更新记录并返回受影响的记录数,其中$update是要更新的数据数组,如:array('name' => 'haha')
  • SQL::insert($table, $data) 插入一条记录并返回其ID,其中$data是一个数组,如:array('name' => 'haha', 'age' => 20)
  • SQL::delete($table, $where) 删除记录;

条件格式:

  • 普通:array('a' => 1, 'b' => 2) 相当于 a=1 AND b=2
  • 指定连接操作符:array('link' => 'OR', 'a' => 1, 'b' => 2) 相当于 a=1 OR b=2
  • 指定比较操作符:array('a' => array('>' => 2)) 相当于 a>2
  • 同一个字段多个条件:array('a' => array('>' => 2, '<' => 5)) 相当于(a>2 AND a < 5)
  • 指定多个条件的连接操作符:array('a' => array('link' => 'OR', '>' => 2, '<' => 5))相当于 (a>2 OR a < 5)

上传文件操作

LeiPHP中提供了一个静态类 UPLOAD 来操作上传文件:

  • UPLOAD::get($filename) 返回指定名称的上传文件信息,该名称为<form>表单中的<input type="file">中的name值,该返回值为一个数组,包含以下项: name(名称), type (MIME类型), size (大小),tmp_name (临时文件名);
  • UPLOAD::move($file, $target) 移动上传的文件到指定位置,第一个参数为UPLOAD::get($filename)的返回值,第二个参数为目标文件名;

调试信息操作

LeiPHP中提供了一个静态类 DEBUG 来操作调试信息,当定义了常量APP_DEBUG时, 会在页面底部输出调试信息:

  • DEBUG::put($msg = '', $title = '') 输出调试信息;
  • DEBUG::get() 取调试信息;
  • DEBUG::clear() 清除所有调试信息;

应用相关操作

LeiPHP中提供了一个静态类 APP 来进行应用相关的操作,及一些公共函数:

  • APP::set($name, $value) 设置
  • APP::get($name) 获取设置值
  • APP::is_set($name) 检查是否有指定设置项
  • APP::encrypt_password ($password) 加密密码,返回一个加盐处理后的MD5字符串,如:FF:15855D447208A6AB4BD2CC88D4B91732:83
  • APP::validate_password ($password, $encrypted) 验证密码,第一个参数为待验证的密码,第二个参数为APP::encrypt_password ($password)返回的字符串,返回TRUEFALSE
  • APP::dump($var) 打印变量结构,一般用于调试;
  • APP::show_error($msg) 显示出错信息;
  • APP::load($filename) 载入依赖的php文件,若不指定后缀名,会自动加上.php,默认以当前php文件为根目录,若文件名以/开头,则以常量APP_ROOT定义的应用目录作为根目录;几种用途:
    • 载入依赖文件:APP::load('xxx.php')
    • 使用php文件存储数据,将其读取出来:$data = APP::load('data.php'),php文件内容:<?php return [1,2]; ?>
    • 使用json文件存储数据,将其读取出来:$data = APP::load('data.json'),json文件内容:[1,2]
  • APP::send_json($data) 返回JSON格式数据;
  • APP::send_json_error($msg, $data = array()) 返回JSON格式的出错信息:{"error":"msg"}
  • APP::auth_encode($string, $key, $expirey) 加密账户验证信息,可指定过期时间;
  • APP::auth_decode($string, $key) 加密账户验证信息;
  • APP::init() 初始化LeiPHP;
  • APP::end() 提前退出;

自动路由

LeiPHP中提供了一个静态类 ROUTER 来进行路由相关的操作:

  • ROUTER::register($path, $function, $is_preg = false) 注册中间件,其中$path为路径前缀,$function为要执行的函数,如果$is_pregtrue表示$path是一个正则表达式;
  • ROUTER::run($dir, $path) 执行自动路由。其中$dir是要自动加载的PHP文件所在的目录,以应用目录APP_ROOT中定义的目录为根目录,默认为action目录,$path是当前请求的路径,默认为$_GET['__path__'];

示例

新建应用统一入口文件:index.php

<?php
require('global.inc.php');
ROUTER::run('action', @$_GET['__path__']);
?>

新建首页处理程序:action/index.php

function method_get() {
  echo 'hello, world';
}

需要配置服务器的URL Rewrite,比如将 /app/(.*) 的所有请求转到/app/index.php?__path__=$1

使用PATH_INFO

<?php
require('global.inc.php');
ROUTER::run('action', @$_SERVER['PATH_INFO']);
?>

通过/index.php/xxxx访问。

Apache的配置示例

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/(.*)$ /app/index.php?%{QUERY_STRING}&__path__=$1 [L]

Nginx的配置示例

if (!-e $request_filename) {
  rewrite "^/app/(.*)" "/app/index.php?%{QUERY_STRING}&__path__=$1" last;
}

SAE的配置示例

handle:
 - rewrite: if(!is_dir() && !is_file() && path~"^app/(.*)") goto "app/index.php?%{QUERY_STRING}&__path__=$1"

当请求 /app/my/action 时,会自动执行文件 /action/my/action.php

如请求 /app/my/action/ ,则自动执行文件 /action/my/action/index.php

License

基于MIT协议发布。

Copyright (c) 2012-2019 Zongmin Lei <[email protected]>
http://ucdok.com

The MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].