Routing

Introduction

Routing in the Streams Platform extends Laravel routing. This documentation assumes you are already familiar with routing in Laravel.

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:

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 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:

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

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

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

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

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:

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

API Routes

Addon service providers mentioned above can also define API routes by defining the $api parameter. All API routes automatically add the auth:api middleware.

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.

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.

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.

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.

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