Skip to content

Instantly share code, notes, and snippets.

@4lun
Created June 1, 2015 15:24
Show Gist options
  • Save 4lun/eda838c0099dadbcb4e8 to your computer and use it in GitHub Desktop.
Save 4lun/eda838c0099dadbcb4e8 to your computer and use it in GitHub Desktop.
Controller for Laravel 5 that loads views from the filesystem based on the URL
<?php namespace App\Http\Controllers;
class PageController extends Controller
{
/**
* Loads page from the filesystem
* @param String $page
* @return Response
*/
public function page($page = 'index')
{
$file = 'pages/'.$page;
$view = view()->exists($file);
if (!$view) {
// Try index
$file = 'pages/'.$page.'/index';
$view = view()->exists($file);
}
if($view) {
return view($file);
}
return abort(404);
}
}
<?php
Route::any('/{page?}', 'PageController@page')
// Accept slashes in parameter
->where('page', '(.*)');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment