# Route Collections

## Introduction

A **route collection** is an object that represents a group of routes. A route collection may include a path prefix to be prepended to the path of each route in the collection. Further, a route collection may accept other route collections as children, effectively creating a routing tree.

The most common use of route collections is to group routes with a common path prefix.

## **Class Synopsis**

```javascript
class RouteCollection
{
    constructor(string prefix, array routes = [], array collections = []);

    addRoute(Route route);
    addRoutes(Route[] routes);
    addCollection(RouteCollection collection);
    addCollections(RouteCollection[] collections);

    findRouteByPath(string path);
    findRouteByName(string name, bool deep = false);
    findCollectionByPrefix(string prefix);

    getRoutes() : Route[];
    getCollections() : RouteCollection[];
    getFlattenedRoutes() : Route[];
    getFlattenedCollections() : RouteCollection[];
    getPathPrefix() : string;
    getParentCollection() : RouteCollection|null;

    setPathPrefix(string prefix);
    setParentCollection(RouteCollection collection);
}
```

## Examples

### **Example: Grouping routes with a shared path prefix**

```javascript
let routes = new RouteCollection('/sandbox', [
    // Matches #/sandbox/index
    new Route('sandbox.index', '/index', 'Sandbox.Sandbox.index'),
    // Matches #/sandbox/demo 
    new Route('sandbox.demo' '/demo', 'Sandbox.Sandbox.demo')
];
```

### **Example: Chaining collections declaratively**

```javascript
let routes = new RouteCollection('/sandbox', [
    // Matches #/sandbox/index
    new Route('sandbox.index', '/index', 'Sandbox.Sandbox.index')  
], [
    new RouteCollection('/section', [
        // Matches #/sandbox/section/demo
        new Route('sandbox.demo', '/demo', 'Sandbox.Sandbox.demo')
    ])
]);
```

### **Example: Chaining collections imperatively**

```javascript
let sandboxRoutes = new RouteCollection('/sandbox', [
    // Matches #/sandbox/index
    new Route('sandbox.index', '/index', 'Sandbox.Sandbox.index')
]);

let sectionRoutes = new RouteCollection('/section', [
    // Matches #/section/demo
    new Route('sandbox.demo', '/demo', 'Sandbox.Sandbox.demo')
]);

sandboxRoutes.addCollection(sectionRoutes);

// sandbox.index still matches #/sandbox/index
// sandbox.demo now matches #/sandbox/section/demo
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.elentra.org/ejs/1.4/guide/routing/route-collections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
