> ## Documentation Index
> Fetch the complete documentation index at: https://runway.duncanmcclean.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Front-end Routing

> Runway can serve up your Eloquent models on the front-end of your site, just like entries.

## Enabling

<Note>Before getting started, ensure you’ve run `php artisan migrate`. Runway comes with a `runway_uris` table which will be used to store all of the front-end URIs.</Note>

First things first, add a `route` key to your resource config, with the URI structure you want to use. Feel free to use Antlers in there for anything dynamic, like a slug or a date.

```php config/runway.php theme={null}
'route' => '/products/{{ slug }}',
```

Next, add the `RunwayRoutes` trait to your Eloquent model.

```php theme={null}
use StatamicRadPack\Runway\Routing\Traits\RunwayRoutes;

class Product extends Model
{
    use RunwayRoutes;
```

If you have any existing models, make sure you run `php please runway:rebuild-uris` to build the ["URIs cache"](#content-uri-cache) Runway uses to map models to URIs.

## Customising the template/layout used

Runway will assume you want to use the `default` template and the `layout` layout for resources.

However, most of the time, you’ll want to change this. To change it, just specify what you want to change them to.

```php config/runway.php theme={null}
'template' => 'products.index',
'layout' => 'layouts.shop',
```

## Available variables

Instead your show/detail view of your model, you’ll have access to a bunch of variables:

* All fields configured in your blueprint (with augmentation of course)
* Any fields not in your blueprint but configured in your model, like `created_at`, `updated_at`.
* Any other variables provided by [the Cascade](https://statamic.dev/cascade#content)

## Static Caching Invalidation

If you're taking advantage of Statamic's [Static Caching](https://statamic.dev/static-caching) functionality, Runway will automatically invalidate the URI of your models on save.

You may also configure additional URIs to be invalidated on save.

```php theme={null}
// config/statamic/static_caching.php

'invalidation' => [

    'class' => null,

    'rules' => [
        'runway' => [
            'product' => [
                'urls' => [
                    '/products',
                    '/products/*',
                ],
            ],
        ],
    ],

],
```

## URI Cache

Since Runway allows you to define your routes using Antlers, much like collections, Runway needs to index all the possible URIs so it can efficiently find the related Eloquent model.

Runway uses the `runway_uris` table to do this. Unless disabled, your application will have a `runway_uris` table, which is responsible for mapping URIs to Eloquent models.

Anytime you create, update or delete an Eloquent model, Runway will update its mappings in the `runway_uris` table.

### Building the URI Cache

When configuring front-end routing in an application with existing models, you should run the `php please runway:rebuild-uris` command to build the Runway's "URI Cache".

If you wish to limit the models being "cached" by the `runway:rebuild-uris` command, you may add the `runwayRoutes` query scope to your model:

```php focus={7-11} theme={null}
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    #[Scope]
	protected function scopeRunwayRoutes(Builder $query): void
	{
		$query->where('is_public', true);
	}
}
```

### Customizing the table name

By default, Runway will use the `runway_uris` table to store the "URI Cache". If you wish to change the name of the table, you may do so in the `runway.php` configuration file:

```php theme={null}
/*
|--------------------------------------------------------------------------
| Runway URIs Table
|--------------------------------------------------------------------------
|
| When using Runway's front-end routing functionality, Runway will store model
| URIs in a table to enable easy "URI -> model" lookups. If needed, you can
| customize the table name here.
|
*/

'uris_table' => 'runway_uris',
```

## Live Preview

Statamic's Live Preview gives you the ability to see what your model will look like in real time as you write and edit.

By default, you'll be able to preview your model at your resource's configured route. However, you are free to customize the available preview targets, just like you can with collections.

```php theme={null}
// config/runway.php

'preview_targets' => [
    ['label' => 'Model', 'url' => '{permalink}'],
    ['label' => 'Index', 'url' => '/products']
],
```

You may use the model's variables in the URL, just like defining a route.

You can learn more about preview targets in the [Statamic documentation](https://statamic.dev/live-preview#preview-targets).
