疾速且简单的PHP微框架

安装: 13

依赖项: 0

建议者: 0

安全性: 0

星星: 1

关注者: 3

分支: 1

开放问题: 0

类型:项目

v1.0.0 2014-09-17 05:49 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:52:36 UTC


README

#{ fw.php } 代码更少,创造更多

你好世界

要创建您的第一个hello world fw.php应用程序,创建index.php并将其添加到其中

$fw->route('GET /@name',
    function($fw, $params) {
        echo'Hello, '.$params['name'].'!';
    }
);

安装

将此存储库复制到您的服务器上的公共可访问文件夹(或如果使用共享托管,则复制到FTP的public_html文件夹)。有多种方法可以获取fw.php

a) 手动下载并解压缩.zip /.tgz

https://github.com/deathbeam/fwphp/archive/master.zip

b) 使用git克隆存储库

git clone https://github.com/deathbeam/fwphp.git /your/public/web/folder

c) 通过Composer获取存储库

composer create-project deathbeam/fwphp /your/public/web/folder dev-master

现在,我们需要安装mod_rewrite,因为它对于.htaccess是必需的。

配置

fw.php可以通过两种方式配置。第一种是仅使用php,第二种是从json文件加载配置。以下示例中,我们将

  • plugins文件夹加载cookie.php扩展
  • 将公共文件的目录从默认的public更改为new_public_dir
  • /路由设置为index函数

仅使用php

这是从index.php中的基本配置。

$fw->set('public_dir', 'new_public_dir');
$fw->cookie = 'cookie.php';
$fw->route('GET /', 'index');

使用json配置文件

以下是配置fw从.json文件的示例

$fw->config('config.json');

以下是config.json的内容

{
	"globals": {
		"public_dir": "new_public_dir"
	},
	"libs": {
		"cookie": "cookie.php"
	},
	"routes": {
		"GET /": "index",
	}
}

模板

fw.php使用PHP作为模板语言的超级简单的模板系统。

绘制模板很简单

$fw->draw('test.php');

模板可以读取由$fw->set方法设置的全局变量。

示例模板

以下,我们将创建简单的模板逻辑。

将进入index.php中路由函数的代码

$fw
	->set('header','This is example header')
	->set('body','Content goes here')
	->set('footer','This is example footer'))
	->draw('default.php');

我们将把下面的代码保存为default.php/public目录

<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>The Default Template</title>
	</head>
	<body>
		<header>
			<h1>You are viewing the default page!</h1>
			<?=$header?>
		</header>
		<section>
			<?=$body?>
		</section>
		<footer>
			<?=$footer?>
		</footer>
	</body>
</html>

路由

在fw.php中,我实现了与F3路由非常相似的路由。功能

  • 动态路由带有命名参数
  • 反向路由
  • 灵活的正则表达式路由
  • REST路由映射

示例路由

// mapping routes
$fw->route('home: GET|POST /', 'home');
$fw->route('GET /users', 'users');
$fw->route('users_show: GET /users/@id', 'showUser');
$fw->route('users_do: POST /users/@id/@action', 'userController->@action');

// provide ReST interface by mapping HTTP requests to class method
$fw->route('MAP /rest', 'some_class');

// default route (404 page)
$fw->route('default', 'error');

// redirect
$fw->reroute('users_show', array('id' => 5));
$fw->reroute('/users');

API

许可证

Copyright (c) 2014 Thomas Slusny

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.