Honovel Honovel Deno

Routing in Honovel

Honovel uses a simple yet powerful routing system inspired by Laravel. Routes are organized into web routes and API routes, keeping your app organized and maintainable.

Route Files

By default, routes are stored in the routes folder:

routes/
├─ web.ts    # Routes for pages, views, and HTML responses
└─ api.ts    # Routes for JSON APIs and backend endpoints

Defining a Web Route

In web.ts, you can define routes using the Route facade:

import { Route } from "Illuminate/Support/Facades/index.ts";

Route.view("/", "welcome"); // Directly returns the "welcome" view with GET method

// Route example with params
Route.get("/docs/{content}", async function({request}, {content}){
    return content; // Returns the 'content' URL parameter
});

Using Controllers

For cleaner code, move route logic into controllers. Example:

import HomeController from "App/Http/Controllers/HomeController.ts";
import ContentController from "App/Http/Controllers/ContentController.ts";

Route.get("/", [HomeController, "index"]);
Route.get("/docs/{content}", [ContentController, "show"]);

Available Route Methods

Honovel’s Route facade supports several methods to define routes, handle multiple HTTP verbs, or group routes:

  • Route.get(path, handler) — Define a GET route. Params:
    • path: URL path, e.g., "/users"
    • handler: Controller array [UserController, "index"] or an inline/async function
  • Route.post(path, handler) — Define a POST route. Same params as get
  • Route.put(path, handler) — Define a PUT route for updating resources
  • Route.delete(path, handler) — Define a DELETE route
  • Route.head(path, handler) — Define a HEAD route (like GET but without response body)
  • Route.any(path, handler) — Responds to all HTTP methods
  • Route.match(methods, path, handler) — Responds to multiple HTTP methods
  • Route.view(path, view) — Directly returns a view without a controller
  • Route.group(options, callback) — Group routes with shared prefixes or middleware
  • Route.resource(uri, controller) — Creates RESTful resource routes automatically

Resource Routes Example

Example: Route.resource("post", PostController) generates the following RESTful routes:

HTTP Method Path Controller Method Route Name
GET /posts PostController.index posts.index
GET /posts/create PostController.create posts.create
POST /posts PostController.store posts.store
GET /posts/{post} PostController.show posts.show
GET /posts/{post}/edit PostController.edit posts.edit
PUT/PATCH /posts/{post} PostController.update posts.update
DELETE /posts/{post} PostController.destroy posts.destroy

Notes:

  • Base path is plural (posts)
  • Route parameters are singular ({post})
  • Route names follow {plural}.{action} convention

Route Parameters

Honovel supports dynamic route parameters in your paths. You can define parameters as required or optional:

  • {routeKey}Required parameter. If the URL does not include this parameter, Honovel automatically executes abort(404).
  • {routeKey?}Optional parameter. If the URL does not include this parameter, the controller method will receive undefined.

Example route definitions:

Route.get("/users/{id}", [UserController, "show"]);   // 'id' is required
Route.get("/posts/{slug?}", [PostController, "show"]); // 'slug' is optional

In your controller, the parameters are passed as method arguments in order:

public async show({ request }, {id}) {
    // 'id' contains the value from the URL
}