thenetimp / simple-rest-server
简单的REST服务器实现。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 06:52:23 UTC
README
这是一个用PHP编写的简单REST服务器。在其他PHP框架中处理相对简单的事情让我感到沮丧后,我创建了Simple Rest Server框架。它不依赖于任何特定的ORM。对程序员的要求很少。它提供了通过http_basic和http_digest认证方法进行认证的方法,并提供了一种扩展安全性的方式,用户可以将它与其他认证方法相结合。
框架的核心存储在src/目录中,由2个目录中的5个文件组成。管理一切的Kernel。扩展以提供功能的ControllerBase。
请求的结构。
假设您的API服务位于https://api.foo.bar/,并且有人想要调用资源/user/profile/1。请求URL包含将请求路由到正确控制器和操作所需的信息。
- user = Controller = app/src/Bundle/Controler/UserController.php
- profile = action = profileGetAction
- 1 = 动作方法的第1个参数
- 额外的部分将成为方法中的额外参数。
请求URI被拆分为这样的。方法、ction和them参数,因此要在/user/profile/1上使用GET方法,UserController中的方法名称为profileGetAction()
<?php
class UserController
{
public function profileGetAction($userId = 0)
{
$data = array();
// Code here to get the data that is needed to get the profile.
// $data is returned at the end and json encoded by
//the kernel $data can be an array or a valid object.
return $data;
}
}
请求URI被拆分为这样的。amethod、ction和them参数,因此要在/user/profile/1/test/34上使用GET方法,UserController中的方法名称为profileGetAction(),接下来的3个参数为
- $userId = 1
- $service = test
- $roleId = 34
在下面的示例中。
<?php
class UserController
{
public function profileGetAction($userId = 0, $service = null, $roleId)
{
$data = array();
// Code here to get the data that is needed to get the profile.
// $data is returned at the end and json encoded by
//the kernel $data can be an array or a valid object.
return $data;
}
}
数据库
数据库连接是设计成你可以使用任何你想要的数据库连接。Doctrine?Propel?adodb?基本的mysql函数调用。所有都支持,因为它取决于你创建代码来连接资源。在database.ini文件中,需要2个参数,其余的参数根据你选择的数据库是可选的。如果"enabled"设置为"true",则SRS将尝试使用在"handler_class"中指定的类创建数据库资源。在下面的示例中,它设置为"Bundle\Component\Database"。
[database]
enabled=true
handler_class=Bundle\Component\Database
username=root
password=
hostname=localhost
database=srs_db
portnumb=3306
类文件"Bundle\Component\Database"可能看起来像这样以连接到mysql数据库。
<?php
namespace Bundle\Component;
use Component\AbstractDatabase as AbstractDatabase;
class Database extends AbstractDatabase
{
public function __construct($dbCfg= array())
{
//connection to the database
$mysqli = new \mysqli($dbCfg['hostname'], $dbCfg['username'], $dbCfg['password'], $dbCfg['database']);
if ($mysqli->connect_errno) {
throw new Exception("Unable to connect to the database with given credentials.");
}
$this->dbResource = $mysqli
}
}
从这一点起,$mysqli资源将在Controller文件中作为$dbr可用。
安全
默认情况下,安全是禁用的。要启用它,您必须更改app/conf/security.ini中的设置
[security]
enabled=false // change to true to enable it.
realm=Secure Area // security realm
security_class=Bundle\Security\HttpDigest // the class to manage security
securit_class必须扩展AbstractSecurity抽象类。控制器在安全类上调用"authorized"方法,如果授权则返回true,如果没有授权则返回false。所有安全工作都必须在该文件中完成。默认情况下,SRS支持2种认证方法,并提供2个模板类以供覆盖。
http_basic认证
SRS附带了一个用于Http基本认证的模板文件。为了使用它,您必须提供validatePasses方法的逻辑。在PHP中,http基本用户名和密码存储在$_SERVER全局变量中,键为'PHP_AUTH_USER'和'PHP_AUTH_PW'。
<?php
namespace Bundle\Security;
use Security\HttpBasicBase as HttpBasicBase;
class HttpBasic extends HttpBasicBase
{
protected function validationPassed()
{
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
// not a real life example of querying the database....
$row = (perform your database query here and get the first row);
if($row['password'] == hash('md5',$password))
{
return true;
}
return false;
}
}
http_digest认证
SRS还附带了一个用于基本http_digest认证的模板。
<?php
namespace Bundle\Security;
use Security\HttpDigestBase as HttpDigestBase;
/**
*
*/
class HttpDigest extends HttpDigestBase
{
/**
* User defined function to allow getting the password from the database.
*/
public function getPasswordHashForUsername($username)
{
// Query a query to get the password hash from the database for the user and
// return it. No other validation is required here as the base class does the validation.
// the password stored in your database MUST adhere to the below algorithm
// hash('md5', $username . ':' . $digestRealm . ':' . $password);
// $digestRealm should always be the same as the "realm" parameter in the security.ini file.
// It is important that this does not change as it will break all previously generated passwords.
// You should however set the realm to something different than the default to help secure your API
return $passwordHash;
}
}
扩展安全性
创建一个扩展安全性的自定义类应该扩展Security\AbstractSecurity类。新的Security类需要一个public function authorized(),如果通过授权过程返回true,如果未通过授权过程返回false。