リファレンス
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 リファレンス を正本とします。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
boot | static boot(): void | void | フレームワークを初期化する |
autoload | static autoload(string $namespace, string $baseDir): void | void | 名前空間とディレクトリのマッピングを追加する |
createConfig | static createConfig(): AppConfig | AppConfig | Web App の設定を組み立てる |
createWebApp | static createWebApp(?AppConfig $config = null): App | App | 設定から Web App を生成する |
Naify\App\App
集約ルート。リクエスト処理のエントリーポイント。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
create | static create(string $basePath = '', ?Router $router = null, ?AppConfig $config = null): static | static | AppとRouterを生成する |
getRouter | getRouter(): Router | Router | 内部で保持しているRouterを取得する |
loadRoute | loadRoute(callable|string $route): static | static | ルート定義を読み込む |
getConfig | getConfig(): AppConfig | AppConfig | 起動設定を取得する |
getMiddleware | getMiddleware(): MiddlewarePipeline | MiddlewarePipeline | グローバルミドルウェアパイプラインを取得する |
run | run(): ResponseInterface | ResponseInterface | リクエスト処理を実行し、レスポンスを返す |
Naify\Router\Router
ルーティングエンジン。URLパターンとハンドラのマッピング。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
any | any(string $pattern, callable|string $handler): Route | Route | 全HTTPメソッドでルート登録 |
get | get(string $pattern, callable|string $handler): Route | Route | GETメソッドでルート登録 |
post | post(string $pattern, callable|string $handler): Route | Route | POSTメソッドでルート登録 |
put | put(string $pattern, callable|string $handler): Route | Route | PUTメソッドでルート登録 |
delete | delete(string $pattern, callable|string $handler): Route | Route | DELETEメソッドでルート登録 |
patch | patch(string $pattern, callable|string $handler): Route | Route | PATCHメソッドでルート登録 |
group | group(string $prefix, callable $callback): RouteGroup | RouteGroup | ルートグループを作成する |
loadRoutes | loadRoutes(callable|string $routes): void | void | ルート定義を読み込みます。Bootstrap では通常 $app->loadRoute() 経由で使う |
urlFor | urlFor(string $name, array $data = []): string | string | 名前付きルートのURLを生成する |
error | error(array|string $codes, callable|string $handler): static | static | HTTPステータスコード別エラーハンドラを登録する |
setBasePath | setBasePath(string $basePath): void | void | ベースパスを設定する |
setErrorHandler | setErrorHandler(callable $handler): static | static | Router層のフォールバックエラー処理を設定する |
createRouteFactory | createRouteFactory(string $namespace = ''): RouteFactory | RouteFactory | "Controller::action" 形式をcallableに変換するファクトリを生成する |
dispatch | dispatch(RequestContext $context): RequestContext | RequestContext | URLマッチングとハンドラ実行 |
match | match(string $method, string $path): ?array | ?array | URLマッチングのみ行う |
Naify\Router\Route
個別ルート。名前やミドルウェアの設定。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
setName | setName(string $name): static | static | ルート名を設定する(urlForで使用) |
trimExtension | trimExtension(int $depth = 1): static | static | 拡張子を無視してマッチングする |
addMiddleware | addMiddleware(MiddlewareInterface $middleware): static | static | ルート固有のミドルウェアを追加する |
Naify\Router\RouteGroup
ルートグループ。共通プレフィックスとミドルウェアの共有。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
any | any(string $pattern, callable|string $handler): Route | Route | 全メソッドでルート登録(グループ内) |
get | get(string $pattern, callable|string $handler): Route | Route | GETルート登録(グループ内) |
post | post(string $pattern, callable|string $handler): Route | Route | POSTルート登録(グループ内) |
group | group(string $prefix, callable $callback): static | static | ネストされたグループを作成する |
addMiddleware | addMiddleware(MiddlewareInterface $middleware): static | static | グループ全体にミドルウェアを追加する |
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との唯一の接点。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
getMethod | getMethod(): string | string | HTTPメソッドを取得する("GET", "POST" 等) |
getPath | getPath(): string | string | リクエストパスを取得する |
getParam | getParam(string $name, mixed $default = null): mixed | mixed | GET/POSTパラメータを取得する |
getUrlParam | getUrlParam(string $name, mixed $default = null): mixed | mixed | URLパラメータ({id} 部分)を取得する |
getCookie | getCookie(string $name, ?string $default = null): ?string | ?string | Cookie値を取得する |
getServer | getServer(string $name, ?string $default = null): ?string | ?string | サーバー変数($_SERVER相当)を取得する |
getRoutingHandler | getRoutingHandler(): ?string | ?string | マッチしたハンドラ(Controllerクラス名)を取得する |
getRoutingAction | getRoutingAction(): ?string | ?string | マッチしたアクション名を取得する |
getRouteName | getRouteName(): ?string | ?string | マッチしたルート名を取得する |
Naify\Router\RequestContextHandlerInterface
Controllerが実装するインターフェース。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
handle | handle(RequestContext $context, ?string $action): void | void | アクションを実行します。$action がnullの場合はController側で解決できる |
Naify\Router\RouteDispatcher
MVCディスパッチャー。RouteFactoryから生成され、RouterがマッチしたルートをControllerとActionに振り分ける内部クラス。
RouteDispatcherはNaify内部で使用されるクラスです。アプリコードから直接生成することは通常ありません。
Naify\Router\Exception\RouteException
HTTPステータスコード付き例外。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
getStatusCode | getStatusCode(): int | int | HTTPステータスコードを取得する |
getMessage | getMessage(): string | string | エラーメッセージを取得する(\Exception継承) |
Naify\Psr\Middleware\MiddlewarePipeline
ミドルウェアパイプライン。リクエスト/レスポンスの前後処理チェーン。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
add | add(MiddlewareInterface $middleware): void | void | ミドルウェアをパイプラインに追加する |
handle | handle(ServerRequestInterface $request): ResponseInterface | ResponseInterface | パイプラインを実行する |
DB層について
NaifyコアはDB実装を抱え込みません。アプリ側でNaifyDbやPDOを選んで組み合わせます。
Naify\Session\Session
セッション管理。名前空間付きストレージを提供。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
getStorage | static getStorage(string $namespace = 'default', int $ttl = 3600, bool $gcTarget = true): Storage | Storage | 名前空間付きストレージを取得する |
start | static start(): bool | bool | セッションを開始する |
gc | static gc(): void | void | 期限切れセッションをガーベージコレクトする |
destroy | static destroy(?string $namespace = null): void | void | セッションを破棄する |
regenerateId | static regenerateId(): bool | bool | セッションIDを再生成する |
Naify\Session\Storage
セッション値の読み書き。ArrayAccess実装。TTL対応。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
get | get(string $key, mixed $default = null): mixed | mixed | 値を取得する |
set | set(string $key, mixed $value): static | static | 値を保存する |
has | has(string $key): bool | bool | キーが存在するか確認する |
delete | delete(string $key): static | static | キーを削除する |
pop | pop(string $key, mixed $default = null): mixed | mixed | 値を取得して削除する(flash用途) |
ArrayAccessインターフェースも実装しているため、$storage['key'] の形式でもアクセスできます。
NaifyView\View\View
NaifyView の描画窓口。テンプレートエンジンを使う場合の標準的な入口。
| メソッド | シグネチャ | 戻り値 | 説明 |
|---|---|---|---|
assign | assign(string $name, mixed $value): static | static | テンプレート変数をアサインする |
assignFromArray | assignFromArray(array $array): static | static | 配列からまとめて変数をアサインする |
setTemplate | setTemplate(string $file): static | static | 描画するテンプレートファイルを指定する |
getTemplate | getTemplate(): ?string | ?string | 現在のテンプレート名を取得する |
render | render(?string $name = null, array $context = []): string | string | テンプレートを描画して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"