---
title: Routing
---

# Routing

<div class="documentation__toc"></div>

## Introduction

Routing in the Streams Platform extends Laravel routing. This documentation assumes you are already familiar with [routing in Laravel](https://laravel.com/docs/routing).

## Defining Routes

### Automatic Routes

Most `admin` routes can be handled automatically. Automated routes must follow a `/admin/{module}/{stream?}/{method?}/{id?}` format.

Below are some examples of what automatic routing can do for you:

```bash
admin/pages => \Anomaly\PagesModule\Http\Controller\PagesController@index
admin/pages/create => \Anomaly\PagesModule\Http\Controller\PagesController@create
admin/pages/edit/{id} => \Anomaly\PagesModule\Http\Controller\PagesController@edit
admin/pages/view/{id} => \Anomaly\PagesModule\Http\Controller\PagesController@view

admin/pages/types => \Anomaly\PagesModule\Http\Controller\TypesController@index
admin/pages/types/create => \Anomaly\PagesModule\Http\Controller\TypesController@create
admin/pages/types/edit/{id} => \Anomaly\PagesModule\Http\Controller\TypesController@edit
admin/pages/types/delete/{id} => \Anomaly\PagesModule\Http\Controller\TypesController@delete
```

> The [make:stream](../reference/cli-commands#make-stream) command leverages automatic routing by default.{.notice}

### Addon Service Provider

Most routing is specified within the relevant addon's service provider using the `$routes` and `$api` properties.

> The theme of a project is a perfect place to define miscellaneous routes.{.tip}

The most basic route definition looks like this:

```php
protected $routes = [
    'example/uri' => 'Example\ExampleModule\Http\Controller\ExampleController@example',
];
```

If you do not need a controller you can route to a view directly:

```php
protected $routes = [
    'example/uri' => 'anomaly.theme.example::pages/example',
];
```

You can optionally define more information by using an array definition.

```php
protected $routes = [
    'posts/{slug}' => [
        'verb' => 'GET',
        'as' => 'anomaly.module.posts::posts.view',
        'uses' => 'Anomaly\PostsModule\Http\Controller\PostsController@view',
        'where' => [
            'slug' => '([A-Za-z])',
        ],
        'middleware' => [
            'Example\Http\Middleware\DoSomething',
        ],
    ],
];
```

#### Map Method

Addon service providers can also use the `map` method to define routes:

```php
public function map()
{
    Route::get('/', function () {
        return view('theme::pages/welcome');
    });
}
```

### API Routes

Addon service providers [mentioned above](#addon-service-provider) can also define API routes by defining the `$api` parameter. All API routes automatically add the `auth:api` middleware.

```php
protected $api = [
    'api/widgets/{slug}' => 'Anomaly\StoreModule\Http\Controller\ApiController@widgets',
];
```

### Routes File

A quick and easy way to start routing is by simply using Laravel's `routes/web.php` file. 

```php
Route::get('/', function () {
    return view('theme::pages/welcome');
});
```


## Route Variables

Route `variables` can be used for various features. The basic principle is passing along information to the handling request from the route. You will be able to access this later in the route: `$request->route()->getAction($variable, $default);`

For example you can define `'csrf' => false,` to bypass `VerifyCsrfToken` middleware.

```php
protected $routes = [
    'example/route' => [
        'csrf' => false,
        'uses' => 'Anomaly\ExampleModule\Http\Controller\ExampleController@handle',
    ],
];
```


## Route Middleware

Route `middleware` can be defined easily within the route definition.

```php
protected $routes = [
    'example/route' => [
        'csrf' => false,
        'uses' => 'Anomaly\ExampleModule\Http\Controller\ExampleController@handle',
        'middleware' => [
            Anomaly\ExampleModule\Http\Middleware\CheckSomething::class,
        ],
    ],
];
```


## Overriding Routes

Named routes will return the `last` matching route. Knowing this we can override `named` routes from custom addons which are loaded last simply by specifying a `name` that was defined earlier.

> Your project's theme is a great way to define overrides. 

The below example overrides the default `view` route for `posts` within the `posts module`. Note that we use the existing controller, we are only overriding the URI.

```php
protected $routes = [
    'press/{slug}' => [
        'as' => 'anomaly.module.posts::posts.view',
        'uses' => 'Anomaly\PostsModule\Http\Controller\PostsController@view',
    ],
];
```
