ColdBox 出力例

このページの内容

ColdBox 出力例

bxSites coldbox が実際に生成するものの例です。以下は すべて、ここに示す小さな Bookshelf アプリケーションに対する実際の実行結果 であり、手書きの説明ではありません。

このアプリケーションは、ルーター、ハンドラー1つ、モデル1つ、WireBox バインダー、スケジューラー、インターセプター、そして独自のルーターと ハンドラーを持つ api モジュールを宣言しています。

class {
	function configure() {
		route( "/" ).as( "home" ).to( "books.index" )
		route( "/books/:id" ).to( "books.show" )
		resources( "books" )
		route( "/old/shelf" ).toRedirect( "/books" )
		route( ":handler/:action?" ).end()
	}
}
/**
 * The public bookshelf endpoints.
 */
class {

	/**
	 * List every book on the shelf.
	 *
	 * @event The request context
	 *
	 * @return the rendered listing
	 */
	function index( event, rc, prc ) {}

	/**
	 * Show one book by id.
	 *
	 * @id The book id
	 */
	function show( event, rc, prc ) {}

	/**
	 * Runs before every action in this handler.
	 */
	function preHandler( event, rc, prc, action, eventArguments ) {}

	private function findOr404( id ) {}
}
class {
	this.title = "Bookshelf API"
	this.author = "Ortus Solutions"
	this.version = "1.0.0"
	this.entryPoint = "api"
	this.dependencies = [ "cbsecurity" ]
}

bxSites coldbox を1回実行すると11ページが生成されます。そのうち5ページを コードブロックではなく実際にレンダリングして示します。

生成されたページは相互にリンクしますが、この抜粋ではリンク先が生成後の サイト内にしか存在しないため、プレーンなコードとして示しています。


routes.md

Routes

16 route(s), in the order the routers declare them - ColdBox matches the first one that fits, so order is meaningful.

VerbsPatternTargetNameModule
ANY/books.indexhome
ANY/books/:idbooks.show
GET/booksbooks.index
GET/books/newbooks.new
POST/booksbooks.create
GET/books/:idbooks.show
GET/books/:id/editbooks.edit
PUT/PATCH/books/:idbooks.update
DELETE/books/:idbooks.delete
ANY/old/shelf/books redirect
ANY:handler/:action?by convention
GET/api/booksbooks.indexapi
POST/api/booksbooks.createapi
GET/api/books/:idbooks.showapi
PUT/PATCH/api/books/:idbooks.updateapi
DELETE/api/books/:idbooks.deleteapi

A route marked by convention has no explicit target: ColdBox resolves the handler and action from the URL pattern's own placeholders.


この表で注目すべき点: resources( "books" ) は ColdBox が生成する7本の ルートに展開され、モジュールの apiResources() はさらに5本に展開されて ModuleConfig が宣言する /api エントリーポイントを反映し、規約ルートは 元のパターンをそのまま保っています。


handlers/Books.md

Books

handlers.Books

The public bookshelf endpoints.

Routes

VerbsPatternAction
ANY/index
ANY/books/:idshow
GET/booksindex
GET/books/newnew
POST/bookscreate
GET/books/:idshow
GET/books/:id/editedit
PUT/PATCH/books/:idupdate
DELETE/books/:iddelete

Actions

Any index( event, rc, prc )

List every book on the shelf.

Reached by /, /books

ParameterTypeRequiredDescription
eventAnynoThe request context
rcAnyno
prcAnyno
  • Returns the rendered listing

Any show( event, rc, prc )

Show one book by id.

Reached by /books/:id, /books/:id

ParameterTypeRequiredDescription
eventAnyno
rcAnyno
prcAnyno

Lifecycle hooks

ColdBox calls these itself around the handler's actions - they aren't reachable as events of their own.

Any preHandler( event, rc, prc, action, eventArguments )

Runs before every action in this handler.

ParameterTypeRequiredDescription
eventAnyno
rcAnyno
prcAnyno
actionAnyno
eventArgumentsAnyno

ルートのセクションは、ルートのターゲットをハンドラーに突き合わせて構成され ます。ハンドラーのページを開いた読者は、どう到達されるか、どの URL がどの アクションを呼ぶかをすぐに把握できます。preHandler はライフサイクル フックとして別扱いになり、private の findOr404 は現れません。


models/index.md

Models

ModelModuleScopeInjects
Booksingleton1

WireBox mappings

Declared in the application's binder. A model with no mapping here is still injectable - WireBox maps the models directory by convention.

AliasMaps toKindScopeModule
BookServicemodels.BookServiceclasssingleton
models.servicesmodels.servicesdirectory

modules/api.md

Bookshelf API

api · mounted at /api

AuthorOrtus Solutions
Version1.0.0
Entry pointapi
Depends oncbsecurity

Routes

VerbsPatternTarget
GET/api/booksbooks.index
POST/api/booksbooks.create
GET/api/books/:idbooks.show
PUT/PATCH/api/books/:idbooks.update
DELETE/api/books/:idbooks.delete

Handlers

  • Books

scheduled-tasks.md

Scheduled tasks

TaskScheduleRunsConstraintsModule
Reindex the shelfevery day at 02:00runEvent( "books.reindex" )one server only

スケジュールタスクの処理内容はクロージャーであってリテラルではないため、 静的に解決できるものはありません。もっとも有用な列を空にする代わりに、 クロージャーのソーステキストをそのまま持ち込んでいます。これはスケジュー ラーが実行する内容そのものです。

設定と、静的な読み取りが意図的に見ないものについては ColdBoxアプリケーション を、同じプロジェクトのクラスが生む API リファレンスについては DocBox 出力例 を参照して ください。

このページを編集 Markdownをダウンロード 最終更新 Sep 11, 2026, 7:11:15 PM