LoginSignup
7

More than 5 years have passed since last update.

他のPHPからBEAR.Sundayリソースを利用する

Last updated at Posted at 2018-05-07

概要

他のCMSやフレームワークで作られたPHPアプリケーションからBEAR.Sundayのリソースを利用するのにHTTPリクエストをする必要はありません。composerのパッケージにして他のシステムからrequireしたあとは1行でリソースを利用可能です。

グローバル定数がない完全なDI設計のために複数のBEAR.Sundayアプリケーションが同一のPHPメモリ空間で存在可能です。

利用例

リソースをjsonで取得したい場合は文字列評価します。

json.php
$json = (string) (new \BEAR\Package\Bootstrap)
->getApp('MyVendor\MyProject', 'prod-app')
->resource->uri('page://self/index')(['name' => 'Sunday']);

リソースを配列で取得したい場合はbodyプロパティを用います。

body.php
$body = (new \BEAR\Package\Bootstrap)
->getApp('MyVendor\MyProject', 'prod-app')
->resource->uri('page://self/index')(['name' => 'Sunday'])->body;

リソースの要素を取得したい場合は配列アクセスします。

greeting.php
$greering = (new \BEAR\Package\Bootstrap)
->getApp('MyVendor\MyProject', 'prod-app')
->resource->uri('page://self/index')(['name' => 'Sunday'])['greeting'];

リソースをHTMLで取得したい場合はhtmlコンテキストを指定します。

html.php
$html = (string) (new \BEAR\Package\Bootstrap)
->getApp('MyVendor\MyProject', 'prod-html-app')
->resource->uri('page://self/index')(['name' => 'Sunday']);

リソースを関数として取得したい場合はメソッドを指定して(get以外の場合)uri()で作成されたリクエストオブジェクトを取得します。

get.php
$get = (new \BEAR\Package\Bootstrap)
->getApp('MyVendor\MyProject', 'prod-app')
->resource->uri('page://self/index');

$json = (string) $get(['name' => 'koriym']);
$greeting = $get(['name' => 'koriym'])['greeting'];
post.php
$post = (new \BEAR\Package\Bootstrap)
->getApp('Polidog\Todo', 'prod-html-app')
->resource->post->uri('page://self/index');

$response = $post(['todo' => 'shopping'];
echo $response->code // 200

結論

BEAR.Sundayのリソースは再利用性に優れています。複数のアプリケーションを依存パッケージにして1つのアプリケーションにする方法は、マイクロサービス化のように並行開発や疎結合というメリットを享受しながら、開発もテストも管理もディプロイも容易です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7