1. Go to this page and download the library: Download bear/resource library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bear / resource example snippets
namespace MyVendor\Sandbox\Blog;
class Author extends ResourceObject
{
public $code = 200;
public $headers = [
'Content-Type' => 'application/json'
];
public $body = [
'id' =>1,
'name' => 'koriym'
];
/**
* @Link(rel="blog", href="app://self/blog/post?author_id={id}")
*/
public function onGet(int $id): static
{
return $this;
}
public function onPost(string $name): static
{
$this->code = 201; // created
// ...
return $this;
}
public function onPut(int $id, string $name): static
{
$this->code = 204; // no content
//...
return $this;
}
public function onDelete($id): static
{
$this->code = 204; // no content
//...
return $this;
}
}
use BEAR\Resource\ResourceInterface;
$resource = (new Injector(new ResourceModule('FakeVendor/Sandbox')))->getInstance(ResourceInterface::class);
$user = $resource->uri('app://self/user')(['id' => 1]); // 'get' request method can be omitted
// get `ResourceRequest` objcet
$user = $resource->get->uri('app://self/user')->withQuery(['id' => 1]);
// assign to the template
echo "User resource body is {$user}"; // same in the template enigne template
?start_inline
use Ray\WebContextParam\Annotation\QueryParam;
class News extends ResourceObject
{
public function foo(#[QueryParam] string $id) : ResourceObject
{
// $id = $_GET['id'];
?start_inline
use Ray\WebContextParam\Annotation\CookieParam;
class News extends ResourceObject
{
#[CookieParam(key: 'id']
public function foo(string $tokenId) : ResourceObject
{
// $tokenId = $_COOKIE['id'];
use Ray\WebContextParam\Annotation\QueryParam;
use Ray\WebContextParam\Annotation\CookieParam;
use Ray\WebContextParam\Annotation\EnvParam;
use Ray\WebContextParam\Annotation\FormParam;
use Ray\WebContextParam\Annotation\ServerParam;
class News extends ResourceObject
{
public function onGet(
#[QueryParam('use_id')] string $userId, // $_GET['use_id'];
#[CookieParam('id')] string $tokenId, // $_COOKIE['id'] or "0000" when unset;
#[EnvParam('app_mode')] string $app_mode, // $_ENV['app_mode'];
#[FormParam('token')] string $token, // $_POST['token'];
#[ServerParam('SERVER_NAME') #server // $_SERVER['SERVER_NAME'];
) : ResourceObject {
?start_inline
use BEAR\Resource\Annotation\ResourceParam;
class News extends ResourceObject
{
/**
* @ResourceParam(param=“name”, uri="app://self//login#nickname")
*/
public function onGet(string $name) : ResourceObject
{
$modules = [new ResourceModule('MyVendor\Sandbox'), new JsonModule]:
$resource = Injector::create(modules)
->getInstance('BEAR\Resource\ResourceInterface');
$userView = (string) $resource->get('app://self/user?id=1');
echo $userView; // get JSON
class User extends ResourceObject
{
public function __construct()
{
$this->setRenderer(new class implements RenderInterface{
public function render(ResourceObject $ro)
{
$ro->headers['content-type'] = 'application/json';
$ro->view = json_encode($ro->body);
return $ro->view;
}
});
}
}
$user = $resource->get('app://self/user?id=1');
$user->transfer(new class implements TransferInterface {
public function __invoke(ResourceObject $ro, array $server)
{
foreach ($ro->headers as $label => $value) {
header("{$label}: {$value}", false);
}
http_response_code($ro->code);
echo $ro->view;
}
);
use BEAR\Resource\ResourceInterface;
// save
$resource = (new Injector(new ResourceModule('FakeVendor/Sandbox')))->getInstance(ResourceInterface::class);
$cachedResource = serialize($resource);
// load
$resource = unserialize($cachedResource);
$news = $resource->get('app://self/news');