Discovering routes for views | laravel-route-discovery | Spatie

 SPATIE

  Laravel Route Discovery
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-route-discovery](https://spatie.be/docs/laravel-route-discovery/v1)  Discovering-routes-for-views  Discovering routes for views

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-route-discovery/v1)

- [ Introduction ](https://spatie.be/docs/laravel-route-discovery/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-route-discovery/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-route-discovery/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-route-discovery/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-route-discovery/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-route-discovery/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-route-discovery/v1/about-us)

Discovering routes for controllers
----------------------------------

- [ Getting started ](https://spatie.be/docs/laravel-route-discovery/v1/discovering-routes-for-controllers/getting-started)
- [ Mapping controllers to routes ](https://spatie.be/docs/laravel-route-discovery/v1/discovering-routes-for-controllers/mapping-controllers-to-routes)

Discovering routes for views
----------------------------

- [ Discovering routes for views ](https://spatie.be/docs/laravel-route-discovery/v1/discovering-routes-for-views/getting-started)
- [ Mapping views to routes ](https://spatie.be/docs/laravel-route-discovery/v1/discovering-routes-for-views/mapping-views-to-routes)

Advanced usage
--------------

- [ Using route transformers ](https://spatie.be/docs/laravel-route-discovery/v1/advanced-usage/using-route-transformers)

 Discovering routes for views
============================

###  On this page

1. [ Via the routes file ](#content-via-the-routes-file)
2. [ Via the config file ](#content-via-the-config-file)

This package can automatically discover and register routes for a directory containing Blade views.

Via the routes file
-----------------------------------------------------------------------------------------------------------------

You can also enable route discovery via the routes file.

```
// in a routes file

use Spatie\RouteDiscovery\Discovery\Discover;

Discover::views()->in(resource_path('views/auto'));
```

To use a prefix, add middleware, and more, you can put that call to `Discover::views()` in a group.

```
// in a routes file

use Spatie\RouteDiscovery\Discovery\Discover;

Route::prefix('my-discovered-views')->group(function() {
    Discover::views()->in(resource_path('views/auto'));
});
```

Via the config file
-----------------------------------------------------------------------------------------------------------------

In the `discover_view_in_directory` key of the `route-discovery` config file, you can specify a directory that contains views.

```
// config/route-discovery.php

// ...

/*
 * Routes will be registered for all views found in these directories.
 * The key of an item will be used as the prefix of the uri.
 */
'discover_views_in_directory' => [
    'docs' => resource_path('views/docs'),
],

// ..
```

Using this example above, routes will be registered for all views in the `resource_path('views/docs')` directory. The key of the item will be used as a prefix. If you don't want to prefix your discovered routes, simply do not use a key.

```
// config/route-discovery.php

'discover_views_in_directory' => [
    resource_path('views/discovery'),
],
```

Of course, you can also discover routes for multiple directories in one go.

```
// config/route-discovery.php

'discover_views_in_directory' => [
    resource_path('views/discovery'),
    resource_path('views/another-directory'),
],
```

If you want to register multiple directories with the same prefix, you can use array syntax

```
// config/route-discovery.php

'discover_views_in_directory' => [
    'docs' => [
        resource_path('views/docs'),
        resource_path('views/other-docs')
    ],
],
```
