AuraプロジェクトはAura.Dispatcherの助けを借りて様々なバリエーションのプロジェクトを取り扱うことができます。
従って、あなたのアプリケーションがまだ小さく、成長している場合でもアプリケーションをマイクロフレームワークからフルスタックスタイルへと変化させてゆくことは簡単です。
You can skip to your favourite usage.
以下に続くのはマイクロフレームワークスタイルのルーターで、アクションロジックはルーターのパラメータに埋め込まれています。modify()
メソッドのなかでaura/web-kernel:response
によってシェアされたaura/web-kernel:request
とaura/web-kernel:response
サービスを取得します。次に、blog.read
という名前をルートに追加しアクションのコードをクロージャとして埋め込みます。
<?php
namespace Aura\Web_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
// ...
public function modify(Container $di)
{
$request = $di->get('aura/web-kernel:request');
$response = $di->get('aura/web-kernel:response');
$router = $di->get('aura/web-kernel:router');
$router
->add('blog.read', '/blog/read/{id}')
->addValues(array(
'action' => function ($id) use ($request, $response) {
$content = "Reading blog post $id";
$response->content->set(htmlspecialchars(
$content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
));
}
));
}
// ...
}
上の例をルーター自身を使う代わりにアクションロジックをディスパッチャーの中におくように修正できます。
blog.name
という名前でディスパッチャにアクションクロージャを設定します。そしてルーターの内部ではディスパッチャーとのマッチングの為にaction
の値に設定します。
<?php
namespace Aura\Web_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
// ...
public function modify(Container $di)
{
$request = $di->get('aura/web-kernel:request');
$response = $di->get('aura/web-kernel:response');
$dispatcher = $di->get('aura/web-kernel:dispatcher');
$dispatcher->setObject(
'blog.read',
function ($id) use ($request, $response) {
$content = "Reading blog post $id";
$response->content->set(htmlspecialchars(
$content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
));
}
);
$router = $di->get('aura/web-kernel:router');
$router
->add('blog.read', '/blog/read/{id}')
->addValues(array(
'action' => 'blog.read',
));
}
// ...
}
マイクロフレームワークスタイルからフルスタックスタイルへと移行することも可能です。(または最初からフルスタックスタイルで開始することも可能です。)
まず、アクションクラスとsrc/
ディレクトリ配下の場所を定義してください。
<?php
/**
* {$PROJECT_PATH}/src/App/Actions/BlogRead.php
*/
namespace App\Actions;
use Aura\Web\Request;
use Aura\Web\Response;
class BlogRead
{
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
public function __invoke($id)
{
$content = "Reading blog post $id";
$this->response->content->set(htmlspecialchars(
$content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
));
}
}
次に、DI Container を通してプロジェクトにどうやって BlogRead を構築するか通知します。_BlogRead_コンストラクタにaura/web-kernel:request
とaura/web-kernel:response
サービスオブジェクトを渡すように_Container_を設定するには、プロジェクトのconfig/Common
ファイルを編集します。
<?php
namespace Aura\Web_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
public function define(Container $di)
{
// ...
$di->params['App\Actions\BlogRead'] = array(
'request' => $di->lazyGet('aura/web-kernel:request'),
'response' => $di->lazyGet('aura/web-kernel:response'),
);
}
// ...
}
その後、 App\Actions\BlogRead
オブジェクトをblog.read
という名前でディスパッチャーにレジーロードで登録します。
<?php
namespace Aura\Web_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
// ...
public function modify(Container $di)
{
// ...
$dispatcher = $di->get('aura/web-kernel:dispatcher');
$dispatcher->setObject(
'blog.read',
$di->lazyNew('App\Actions\BlogRead')
);
}
// ...
}
… そして最後に、ルーターにblog.read
アクションオブジェクトにを指定します。
<?php
namespace Aura\Web_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
// ...
public function modify(Container $di)
{
// ...
$router = $di->get('aura/web-kernel:router');
$router
->add('blog.read', '/blog/read/{id}')
->addValues(array(
'action' => 'blog.read',
));
}
// ...
}