Created
June 1, 2015 15:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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