PHPackages                             pfinal/leaf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. pfinal/leaf

ActiveLibrary

pfinal/leaf
===========

leaf

v2.6.12(3y ago)246225[1 issues](https://github.com/pfinal/leaf/issues)1MITPHPPHP &gt;=5.4CI failing

Since Mar 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/pfinal/leaf)[ Packagist](https://packagist.org/packages/pfinal/leaf)[ Docs](http://www.pfinal.cn)[ RSS](/packages/pfinal-leaf/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (16)Versions (89)Used By (1)

Leafphp 快速入门
============

[](#leafphp-快速入门)

安装
--

[](#安装)

```
composer create-project pfinal/leafphp demo

```

交流QQ群
-----

[](#交流qq群)

```
17778706

```

编码规范
----

[](#编码规范)

- 遵循PSR-1、PSR-2、PSR3、PSR-4、PSR-16 等标准
- PHP代码文件必须以 ``
- PHP代码文件必须使用 不带BOM的 UTF-8 编码
- 命名空间与目录对应，文件名采用"类名.php"格式，目录大小写与命令空间相同，类名大小写与文件名相同(PSR-4规范)
- 类的命名必须遵循大写开头的驼峰命名规范(例如`UserController`)
- 方法名称如果是多个单词,必须遵循小写开头的驼峰命名规范(例如`createOrder`)
- 类中的常量所有字母都必须大写，多个单词间用下划线分隔
- 普通目录全小写格式，多个单词之间用中杠分隔(例如 `views/member-profile`)

基本概念约定
------

[](#基本概念约定)

- 项目 project
- 应用 application
- 模块 bundle
- 控制器 controller
- 动用 action
- 路由 route

目录结构
----

[](#目录结构)

- config 配置文件目录
    - app.php 应用配置文件
    - routes.php 路由
- runtime 运行时目录，存放缓存、日志、调式信息等，需要写权限
    - .gitignore 版本管理工具git忽略清单,请勿删除此文件
- views 视图文件目录
- document 文档
- src PHP源代码
- tests 测试
- vendor 第三方代码 composer管理,请勿手动修改该目录内容
- web 项目发布的根目录
    - static 第三方静态资源目录
    - themes 主题目录，项目的css、js、img等存放在此目录
    - index.php 前端控制器(MVC模式的入口文件)
    - assets 框架自动管理的资源文件,请勿手动修改该目录内容
    - temp 临时文件目录，需要写权限
    - uploads 文件上传目录，需要写权限
- .gitignore 版本管理工具git忽略清单
- console 命令行入口文件
- phpunit 单元测试入口文件

开启调式模式
------

[](#开启调式模式)

nginx配置
-------

[](#nginx配置)

```
# ...

server{

	# ...

	root /wwwroot/project/web/;

	location / {
	     index index.html index.php;
	     try_files $uri @rewrite;
	}

	location @rewrite {
	     rewrite ^/(.*)$ /index.php/$1 last;
	}

	location ~ \.php(/|$) {
	    fastcgi_pass   127.0.0.1:9000;
	    fastcgi_split_path_info ^(.+\.php)(.*)$;
	    fastcgi_param   PATH_INFO $fastcgi_path_info;
	    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	    include        fastcgi_params;
	}

}
```

路由
--

[](#路由)

下面以demo项目为例

在 `config/routes.php` 中定义路由

基本 GET 路由，浏览器访问 `http://localhost/demo/web`

```
use Leaf\Route;
Route::get('/', function(){
    return 'Hello Leafphp!';
});
```

只允许POST请求的路由，以POST方法访问 `http://localhost/demo/web/foo`

```
Route::post('foo', function(){
    return 'post only';
});
```

注册路由响应任意方式的HTTP请求，不对请求方法进行限制，post或get均可，浏览器访问 `http://localhost/demo/web/bar`

```
Route::any('bar', function(){
    return 'any';
});
```

基础路由参数 浏览器访问 `http://localhost/demo/web/user/1`

```
Route::get('user/:id', function($id){
    return 'user id: ' . $id;
});
```

方法注入，框架将自动注入`Leaf\Request`实例，浏览器访问 `http://localhost/demo/web/foo?name=leaf`

```
use Leaf\Request;
Route::any('foo', function(Request $request){
    $name = $request->get('name');
    return $name; // leaf
});
```

支持注入的对象

```
Leaf\Request
Leaf\Application
```

生成URL

```
use Leaf\Url;
$url = Url::to('foo');                 /demo/web/foo
$url = Url::to('foo', ['id' => 1]);    /demo/web/foo?id=1
$url = Url::to('foo', true);           http://localhost/demo/web/foo
```

如果访问时url中没有隐藏入口文件`index.php`,则生成的url也会自动包含`index.php`

请求与响应
-----

[](#请求与响应)

### Request

[](#request)

```
use Leaf\Request;
Route::any('test', function (Request $request) {

    //GET POST PUT DELETE HEAD ...
    $method =  $request->getMethod();

	$bool = $request->isMethod('post')    // post request
	$bool = $request->isXmlHttpRequest()  // ajax request

	$id = $request->get('id');   // $_POST['id'] or  $_GET['id']
	$arr = $request->all();      // $_POST + $_GET

});
```

### Response

[](#response)

```
use Leaf\View;
use Leaf\Response;
use Leaf\Json;
Route::any('/', function(){

    //string
    return 'Hello Leaf!';
    return new Response('Hello Leaf!');

    //view
    return View::render('home.twig'); // 输出views/home.twig模板中内容

    //json
    return Json::render(['status' => true, 'data' => 'SUCCESS']);
    return Json::renderWithTrue('SUCCESS');
    return Json::renderWithTrue('ERROR');
});
```

重定向

```
use Leaf\Redirect;
return Redirect::to('foo');                    // 跳转到 Url::to('foo')
return Redirect::to('/');                      // 项目的根目录
return Redirect::to('http://www.example.com'); // 外部链接
```

中间件
---

[](#中间件)

编写中间件类 `src/Middleware/TestMiddleware.php`

```
