Camron Cade

Artisan web development, mostly using Laravel.

Cleanly handle the routing for static pages in Laravel/Lumen

I'm working on a small project using Laravel's Lumen that requires a few basic static pages. I was looking for a quicker and more elegant way to define each route and felt like I was repeating myself with every $app->get() function I was writing.

I extracted the pages into an array and a left the instantiating to a for loop and I think it's a bit more expressive and elegant. Here is the code I've put the routes.php file:

$pages = [
    '/'        => 'home',
    '/about'   => 'about',
    '/contact' => 'contact',
];

foreach ($pages as $uri => $view)  
{
    $app->get($uri, function () use ($view) {
        return view($view);
    });
}

This seems, to me, to be much simpler and readable than having a long list of $app->get() functions.

What are your thoughts? Do any of you do anything special to handle the routing of your static pages?