Esempio di output ColdBox

In questa pagina

Esempio di output ColdBox

Un esempio reale di ciò che produce bxSites coldbox. Tutto quello che segue proviene da un'esecuzione vera sulla piccola applicazione Bookshelf abbozzata qui: niente è scritto a mano.

L'applicazione dichiara un router, un handler, un model, un binder WireBox, uno scheduler, un interceptor e un modulo api con router e handler propri:

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" ]
}

Una sola esecuzione di bxSites coldbox scrive undici pagine. Cinque sono riprodotte qui sotto, renderizzate davvero anziché come blocchi di codice.

Le pagine generate si collegano tra loro; in questi estratti quei riferimenti compaiono come codice semplice, perché i loro target esistono solo dentro un sito generato.


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.index—api
POST/api/booksbooks.create—api
GET/api/books/:idbooks.show—api
PUT/PATCH/api/books/:idbooks.update—api
DELETE/api/books/:idbooks.delete—api

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


Da notare in quella tabella: resources( "books" ) si è espanso nelle sette rotte che ColdBox genera, l'apiResources() del modulo in altre cinque, con l'entry point /api dichiarato dal suo ModuleConfig, e la rotta per convenzione ha mantenuto il suo pattern letterale.


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

La sezione Rotte nasce associando i target delle rotte all'handler: chi apre una pagina di handler vede subito come viene raggiunto e quale azione colpisce ogni URL. preHandler resta a parte come l'hook di ciclo di vita che è, e il privato findOr404 non compare mai.


models/index.md

Models

ModelModuleScopeInjects
Book—singleton1

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—

Il lavoro di un task pianificato è una closure, non un letterale: staticamente non c'è nulla da risolvere. Invece di lasciare vuota la colonna più utile, il testo sorgente della closure viene riportato così com'è, ed è esattamente ciò che lo scheduler eseguirà.

Vedi Applicazioni ColdBox per la configurazione e per ciò che la lettura statica non può vedere, e Esempio di output DocBox per il riferimento API che producono le classi dello stesso progetto.

Modifica questa pagina Scarica Markdown Ultimo aggiornamento Sep 11, 2026, 7:11:15 PM