Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Created October 27, 2015 15:30
Show Gist options
  • Save kevinruscoe/28a9e707d32a17eab16f to your computer and use it in GitHub Desktop.
Save kevinruscoe/28a9e707d32a17eab16f to your computer and use it in GitHub Desktop.
<?php
return [
'routes' => [
'post' => [
'route' => '/post/{blog}',
'identifier' => 'slug'
],
'page' => [
'route' => '/{page}',
'identifier' => 'slug'
]
]
]
?>
<?php
namespace App;
use Router;
use Route;
class ConfigRouteBinder {
function bind(){
$this->registerRoutes();
}
private function queryContent($type, $identifier_type, $identifier_value){
$content = Content::whereType($type)->where($identifier_type, $identifier_value)->first();
if( !$content ){
abort(404);
}
return $content;
}
private function registerRoutes(){
foreach( config('cms.routes') as $route => $meta ){
Route::get($meta['route'], function($identifier) use ($route, $meta){
$content = $this->queryContent($route, $meta['identifier'], $identifier);
return (new \App\Http\Controllers\ContentController)->show($content);
});
Route::post($meta['route'], function($identifier) use ($route, $meta){
$content = $this->queryContent($route, $meta['identifier'], $identifier);
return (new \App\Http\Controllers\ContentController)->show($content);
});
}
}
}
?>
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
(new App\ConfigRouteBinder)->bind();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment