viloveul/http

Viloveul的Http组件,实现了PSR HTTP Message规范,基于Zend Diactoros

v1.0.7 2019-06-04 19:23 UTC

This package is auto-updated.

Last update: 2024-09-05 07:36:58 UTC


README

确保您的PHP版本 > 7.0

composer require viloveul/http

如何使用

require __DIR__ . '/vendor/autoload.php';

// e.x : http://yourdomain.com/path/to/something?param1=data1&param2=data2

$request = Viloveul\Http\Server\RequestFactory::fromGlobals();

$param1 = $request->getQuery('param1', 'nothing');
$param2 = $request->getQuery('param2', 'nothing');
$param3 = $request->getQuery('param3', 'nothing');

var_dump($param1, $param2, $param3);

// OR you can assign to object

class SampleParam implements Viloveul\Http\Contracts\ServerRequestAssignment
{
	protected $attributes = [];
	public function setAttributes(array $attributes): void
	{
		$this->attributes = $attributes;
	}
	public function getAttributes(): array
	{
		return $this->attributes;
	}
}

$data = $request->loadQueryTo(new SampleParam);

var_dump($data);

可用方法

  • getQuery(key)
  • getPost(key)
  • getServer(key)
  • loadQueryTo(object)
  • loadPostTo(object)
  • loadServerTo(object)