Skip to content

Instantly share code, notes, and snippets.

@juliobitencourt
Last active June 5, 2020 16:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliobitencourt/3693d9eefad5fd1e0a57 to your computer and use it in GitHub Desktop.
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
<?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