Last active
June 5, 2020 16:33
-
-
Save juliobitencourt/3693d9eefad5fd1e0a57 to your computer and use it in GitHub Desktop.
A Laravel controller to serve Static pages. It translates the slug to a view file, but you might override this behavior with yout own methods
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\Web; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
class StaticController extends Controller | |
{ | |
public function show($slug) { | |
$methodName = camel_case($slug); | |
if ( ! method_exists($this, $methodName)) { | |
return $this->loadViewFromSlug($slug); | |
} | |
return $this->$methodName(); | |
} | |
// You can override a slug2view behavior | |
protected function yourCustomMethod() | |
{ | |
// Return whatever you want | |
return view('layouts.static.your_view'); | |
} | |
protected function loadViewFromSlug($slug) | |
{ | |
$slug = $this->slugToSnakeCase($slug); | |
try { | |
return view("layouts.static.{$slug}"); | |
} catch (\InvalidArgumentException $e) { | |
abort(404); | |
} | |
} | |
protected function slugToSnakeCase($slug) { | |
return str_replace('-', '_', $slug); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment