lastguest/aleph

一个非常简单的用于非常小型站点的PHP框架

1.0.0 2014-12-04 00:53 UTC

This package is auto-updated.

Last update: 2024-09-16 20:54:46 UTC


README

![Gitter](https://badges.gitter.im/Join Chat.svg)

Latest Stable Version Latest Unstable Version License

Aleph是一个非常简单的用于非常小型站点的PHP框架。

安装

通过 composer 安装

$ composer require lastguest/aleph

或者仅下载 框架文件

或者远程包含框架文件:(需要在php.ini中设置 allow_url_include = true

<?php
include "https://raw.githubusercontent.com/lastguest/aleph/master/src/aleph.php";

文档

Bootstrap

包含composer vendor/autoload.php

<?php
include 'vendor/autoload.php';

或者在您的入口控制器中直接包含aleph.php文件

<?php
include 'aleph.php';

URL路由

// The index route
get('/',function(){
  echo "<h1>Hello!</h1>";
});

// Listen POST on /
post('/',function(){
  echo "<h1>Received POST Data:</h1><pre>";
  print_r($_POST);
  echo "</pre>";
});

如果您返回一个数组或对象,它将作为JSON提供

get('/api/todos',function(){
  return [
    [ "id"=>1, "text"=>"Write documentation" ],
    [ "id"=>2, "text"=>"Smile" ],
    [ "id"=>3, "text"=>"Play more games" ],
    [ "id"=>4, "text"=>"Conquer the World" ],
  ];
});

响应将是

[
    {
        "id": 1,
        "text": "Write documentation"
    },
    {
        "id": 2,
        "text": "Smile"
    },
    {
        "id": 3,
        "text": "Play more games"
    },
    {
        "id": 4,
        "text": "Conquer the World"
    }
]

数据库

初始化数据库DSN

database('init','mysql:host=localhost;dbname=test','root','root');

运行查询并获取单列

$uid = sql_value('select id from users where username = ? limit 1', array($username));

运行查询并获取单行

$user = sql_row('select * from users where username = ?', array($username));
echo $user->email;

运行查询并迭代所有返回的行

sql_each('select * from users', function($user){
  echo "<li><a href="mailto:{$user->email}">{$user->name}</a></li>";
});

传递参数

sql_each('select * from users where activated = ?', function($user){
  echo "<li><a href="mailto:{$user->email}">{$user->name}</a></li>";
}, array('YES'));

执行SQL命令

if ( sql('delete from users where id = ?',array(123)) ) echo "User deleted.";

服务

Service函数是一个小的DI容器。

注册一个工厂方法

class TestService {
	public $value;
	function __construct($x){ $this->value = $x; }
}

service('test',function($x){
	return new TestService($x);
});

创建服务实例

$a = service('test',3);
$b = service('test',5);
[{"value":3},{"value":5}]

注册一个单例服务

service('test',function($x){
	static $instance = null;
	return $instance === null ? $instance = new TestService($x) : $instance;
});

现在如果我们多次调用 service('test') 函数,我们将每次都得到单例实例

$a = service('test',3);
$b = service('test',5);
$c = service('test');
[{"value":3},{"value":3},{"value":3}]

======================

贡献

如何参与

  1. 星标 项目!
  2. 通过 GitHub issues 回答问题
  3. 报告您发现的错误

核心遵循 GitFlow分支模型。始终反映生产就绪状态的 master 分支,而最新的开发活动在 develop 分支上进行。

每次您想要修复或添加新功能时,请基于 develop 分支创建一个新的分支: git checkout -b BRANCH_NAME develop。只有提交到 develop 分支的请求才会被合并。

非常欢迎 pull requests

解决问题。功能很棒,但更棒的是清理和修复您发现的代码中的问题。

版本控制

核心使用 语义版本规范(SemVer) 进行维护。

版权和许可

版权所有 2014 Stefano Azzolini,许可协议为 MIT

Bitdeli Badge