Catch-All View Routing in Laravel

Published: July 06, 2015

I'm currently working on a project with a designer/front-end developer hybrid who has very little experience with web frameworks and confusing back-end-developer-ish stuff in general. However, we want this developer to be able to sync design changes to our main Git repository, instead of us needing to pull the templates in from a separate FTP site or design repository.

Ideally, we could have a setup that would allow the developer to flesh out new app and marketing screens without introducing an extra step of syncing HTML, CSS, and Javascript changes. Thankfully, we came up with a "catch-all" route that makes it so a developer can add new views without needing to create a controller or handle any back-end code.

Creating a Catch-all Route

Our solution involves setting up a route in our Laravel app that will take the URL path and convert it into a corresponding view at the same path. Now, you might already know that you can use Laravel's view() helper to return views to the user with a dot syntax that corresponds to the file system. So I can return view('people.david') and Laravel will return the view located at resources/views/people/david.blade.php. But what makes this solution so elegant is that the view helper can also take a path with slashes in it like view('people/david'). This makes it really easy to have a route like this:

Route::group(['middleware' => 'auth'], function () {
  Route::get('{view}', function ($view) {
    try {
      return view($view);
    } catch (\Exception $e) {
  })->where('view', '.*');

We placed this is at the end of routes.php. First, we secure the route by applying the auth middleware. Laravel will then take everything after the top-level domain (for example, a path like, and pass it to the closure as a string. We can then attempt to return the view located at contact/thank-you.php or contact/thank-you.blade.php. If Laravel can't find the view at that location, we catch the error and show the standard 404 screen.

We're going to give this a go and see if this works out in the long run, but we think this should serve our needs just fine.

David Hemphill

About David Hemphill

Builder of web things. Family man. I used to fight. I used to music. I'm an introvert, but I still like you.