リファレンス

Naifyコアの主要public APIをまとめたリファレンス。

パッケージ構成

Naify/ ├── Naify.php ├── App/ │ ├── App.php │ ├── AppConfig.php │ └── RequestHandler.php ├── Router/ │ ├── Router.php │ ├── Route.php │ ├── RouteGroup.php │ ├── RouteFactory.php │ ├── RouteDispatcher.php │ ├── RequestContext.php │ ├── RequestContextHandlerInterface.php │ └── Exception/ │ └── RouteException.php ├── Psr/ │ └── Middleware/ │ └── MiddlewarePipeline.php └── Session/ ├── Session.php └── Storage.php

Naify\Naify

フレームワークのエントリーポイント。起動、オートロード設定、Webアプリ生成を担います。詳細な最新 API は 自動生成 API リファレンス を正本とします。

メソッドシグネチャ戻り値説明
bootstatic boot(): voidvoidフレームワークを初期化する
autoloadstatic autoload(string $namespace, string $baseDir): voidvoid名前空間とディレクトリのマッピングを追加する
createConfigstatic createConfig(): AppConfigAppConfigWeb App の設定を組み立てる
createWebAppstatic createWebApp(?AppConfig $config = null): AppApp設定から Web App を生成する

Naify\App\App

集約ルート。リクエスト処理のエントリーポイント。

メソッドシグネチャ戻り値説明
createstatic create(string $basePath = '', ?Router $router = null, ?AppConfig $config = null): staticstaticAppとRouterを生成する
getRoutergetRouter(): RouterRouter内部で保持しているRouterを取得する
loadRouteloadRoute(callable|string $route): staticstaticルート定義を読み込む
getConfiggetConfig(): AppConfigAppConfig起動設定を取得する
getMiddlewaregetMiddleware(): MiddlewarePipelineMiddlewarePipelineグローバルミドルウェアパイプラインを取得する
runrun(): ResponseInterfaceResponseInterfaceリクエスト処理を実行し、レスポンスを返す

Naify\Router\Router

ルーティングエンジン。URLパターンとハンドラのマッピング。

メソッドシグネチャ戻り値説明
anyany(string $pattern, callable|string $handler): RouteRoute全HTTPメソッドでルート登録
getget(string $pattern, callable|string $handler): RouteRouteGETメソッドでルート登録
postpost(string $pattern, callable|string $handler): RouteRoutePOSTメソッドでルート登録
putput(string $pattern, callable|string $handler): RouteRoutePUTメソッドでルート登録
deletedelete(string $pattern, callable|string $handler): RouteRouteDELETEメソッドでルート登録
patchpatch(string $pattern, callable|string $handler): RouteRoutePATCHメソッドでルート登録
groupgroup(string $prefix, callable $callback): RouteGroupRouteGroupルートグループを作成する
loadRoutesloadRoutes(callable|string $routes): voidvoidルート定義を読み込みます。Bootstrap では通常 $app->loadRoute() 経由で使う
urlForurlFor(string $name, array $data = []): stringstring名前付きルートのURLを生成する
errorerror(array|string $codes, callable|string $handler): staticstaticHTTPステータスコード別エラーハンドラを登録する
setBasePathsetBasePath(string $basePath): voidvoidベースパスを設定する
setErrorHandlersetErrorHandler(callable $handler): staticstaticRouter層のフォールバックエラー処理を設定する
createRouteFactorycreateRouteFactory(string $namespace = ''): RouteFactoryRouteFactory"Controller::action" 形式をcallableに変換するファクトリを生成する
dispatchdispatch(RequestContext $context): RequestContextRequestContextURLマッチングとハンドラ実行
matchmatch(string $method, string $path): ?array?arrayURLマッチングのみ行う

Naify\Router\Route

個別ルート。名前やミドルウェアの設定。

メソッドシグネチャ戻り値説明
setNamesetName(string $name): staticstaticルート名を設定する(urlForで使用)
trimExtensiontrimExtension(int $depth = 1): staticstatic拡張子を無視してマッチングする
addMiddlewareaddMiddleware(MiddlewareInterface $middleware): staticstaticルート固有のミドルウェアを追加する
パターン構文({id}, {id:\d+}, [optional])については ルートパターン構文 を参照してください。

Naify\Router\RouteGroup

ルートグループ。共通プレフィックスとミドルウェアの共有。

メソッドシグネチャ戻り値説明
anyany(string $pattern, callable|string $handler): RouteRoute全メソッドでルート登録(グループ内)
getget(string $pattern, callable|string $handler): RouteRouteGETルート登録(グループ内)
postpost(string $pattern, callable|string $handler): RouteRoutePOSTルート登録(グループ内)
groupgroup(string $prefix, callable $callback): staticstaticネストされたグループを作成する
addMiddlewareaddMiddleware(MiddlewareInterface $middleware): staticstaticグループ全体にミドルウェアを追加する

Naify\Router\RouteFactory

Controllerディスパッチ用のハンドラ生成ファクトリ。通常は Router::createRouteFactory() 経由で取得します。

メソッドシグネチャ戻り値説明
__construct__construct(string $namespace = '')-Controllerの名前空間プレフィックスを設定する
__invoke__invoke(string $handler): \Closure\Closure"Controller::action" 形式からハンドラを生成します。$routeFactory('Ctrl::act') で呼べる

Naify\Router\RequestContext

リクエスト情報へのアクセス手段。PSR-7との唯一の接点。

メソッドシグネチャ戻り値説明
getMethodgetMethod(): stringstringHTTPメソッドを取得する("GET", "POST" 等)
getPathgetPath(): stringstringリクエストパスを取得する
getParamgetParam(string $name, mixed $default = null): mixedmixedGET/POSTパラメータを取得する
getUrlParamgetUrlParam(string $name, mixed $default = null): mixedmixedURLパラメータ({id} 部分)を取得する
getCookiegetCookie(string $name, ?string $default = null): ?string?stringCookie値を取得する
getServergetServer(string $name, ?string $default = null): ?string?stringサーバー変数($_SERVER相当)を取得する
getRoutingHandlergetRoutingHandler(): ?string?stringマッチしたハンドラ(Controllerクラス名)を取得する
getRoutingActiongetRoutingAction(): ?string?stringマッチしたアクション名を取得する
getRouteNamegetRouteName(): ?string?stringマッチしたルート名を取得する

Naify\Router\RequestContextHandlerInterface

Controllerが実装するインターフェース。

メソッドシグネチャ戻り値説明
handlehandle(RequestContext $context, ?string $action): voidvoidアクションを実行します。$action がnullの場合はController側で解決できる

Naify\Router\RouteDispatcher

MVCディスパッチャー。RouteFactoryから生成され、RouterがマッチしたルートをControllerとActionに振り分ける内部クラス。

RouteDispatcherはNaify内部で使用されるクラスです。アプリコードから直接生成することは通常ありません。

Naify\Router\Exception\RouteException

HTTPステータスコード付き例外。

メソッドシグネチャ戻り値説明
getStatusCodegetStatusCode(): intintHTTPステータスコードを取得する
getMessagegetMessage(): stringstringエラーメッセージを取得する(\Exception継承)

Naify\Psr\Middleware\MiddlewarePipeline

ミドルウェアパイプライン。リクエスト/レスポンスの前後処理チェーン。

メソッドシグネチャ戻り値説明
addadd(MiddlewareInterface $middleware): voidvoidミドルウェアをパイプラインに追加する
handlehandle(ServerRequestInterface $request): ResponseInterfaceResponseInterfaceパイプラインを実行する

DB層について

NaifyコアはDB実装を抱え込みません。アプリ側でNaifyDbやPDOを選んで組み合わせます。

Naify\Session\Session

セッション管理。名前空間付きストレージを提供。

メソッドシグネチャ戻り値説明
getStoragestatic getStorage(string $namespace = 'default', int $ttl = 3600, bool $gcTarget = true): StorageStorage名前空間付きストレージを取得する
startstatic start(): boolboolセッションを開始する
gcstatic gc(): voidvoid期限切れセッションをガーベージコレクトする
destroystatic destroy(?string $namespace = null): voidvoidセッションを破棄する
regenerateIdstatic regenerateId(): boolboolセッションIDを再生成する

Naify\Session\Storage

セッション値の読み書き。ArrayAccess実装。TTL対応。

メソッドシグネチャ戻り値説明
getget(string $key, mixed $default = null): mixedmixed値を取得する
setset(string $key, mixed $value): staticstatic値を保存する
hashas(string $key): boolboolキーが存在するか確認する
deletedelete(string $key): staticstaticキーを削除する
poppop(string $key, mixed $default = null): mixedmixed値を取得して削除する(flash用途)

ArrayAccessインターフェースも実装しているため、$storage['key'] の形式でもアクセスできます。

NaifyView\View\View

NaifyView の描画窓口。テンプレートエンジンを使う場合の標準的な入口。

メソッドシグネチャ戻り値説明
assignassign(string $name, mixed $value): staticstaticテンプレート変数をアサインする
assignFromArrayassignFromArray(array $array): staticstatic配列からまとめて変数をアサインする
setTemplatesetTemplate(string $file): staticstatic描画するテンプレートファイルを指定する
getTemplategetTemplate(): ?string?string現在のテンプレート名を取得する
renderrender(?string $name = null, array $context = []): stringstringテンプレートを描画してHTML文字列を返す

ルートパターン構文リファレンス

構文例マッチするURL説明
{param}/users/{id}/users/42必須パラメータ。任意の文字列にマッチ
{param:regex}/users/{id:\d+}/users/42正規表現付きパラメータ。数値のみにマッチ
[optional]/hello/[{name}]/hello/, /hello/Worldオプショナルセグメント。省略可能
固定パス/about/about完全一致
ワイルドカード*すべてerror() 用。全ステータスコードにマッチ

組み合わせ例

//必須パラメータ
$router->get('/users/{id}', $handler);

//正規表現付き(数値のみ)
$router->get('/users/{id:\d+}', $handler);

//オプショナル
$router->get('/posts/[{page:\d+}]', $handler);

//名前付きルート
$router->get('/users/{id}', $handler)->setName('user.show');
$url = $router->urlFor('user.show', ['id' => 42]);
// => "/users/42"