1. Go to this page and download the library: Download ngyuki/ritz 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/ */
ngyuki / ritz example snippets
use function DI\value;
use FastRoute\RouteCollector;
use App\Controller\HomeController;
use App\Controller\UserController;
return [
'app.routes' => value(function(RouteCollector $r) {
$r->get('/', [HomeController::class, 'indexAction']);
$r->get('/user/{id}', [UserController::class, 'showAction']);
}),
];
// routes.php
return [
'app.routes' => value(function(RouteCollector $r) {
$r->get('/', [HomeController::class, 'indexAction', 'attr' => 'val']);
}),
];
// HomeController.php
class HomeController
{
public function attrAction(ServerRequestInterface $request)
{
$attr = $request->getAttribute('attr'); // val
}
}
public function showAction(
// DI コンテナから取得
UserRepository $userRepository,
// リクエストオブジェクト
ServerRequestInterface $request,
// リクエストのアトリビュートに設定されたルートパラメータ
$id
) {
return ['user' => $userRepository->get($id)];
}
// ミドルウェアでリクエストに属性を追加
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
$requset = $requset->withAttribute('foo', 987);
$requset = $requset->withAttribute(Bar::class, new Bar());
return $delegate->process($request);
}
// アクションメソッドに注入される
public function showAction($foo, Bar $bar)) {
// ...
}
// ViewModel オブジェクトを返す
return new ViewModel();
// テンプレートにアサインする変数を指定する
return new ViewModel(['val' => 123]);