Regex for all routes plus index/home page?

Hello,

I’m building a simple 3 page site with Slim. Due to the simplicity of the app, I want just one route for all pages.

I think I am close with this:

$app->get('/{path:route1|route2|route3}', function($request, $response, $args) {
    //$route = $args['route'];
    return $this->view->render($response, 'home.phtml'); // This change depending on route.
})->setName('all');

I am able to access /route1, /route2 and /route3.

How can include a match for the root homepage (i.e., no route params)?

Thanks!

This appears to work:

$controller = function($request, $response, $args) {
	return $this->view->render($response, 'home.phtml');
};

$app->get('/', $controller)->setName('home');
$app->get('/{path:route1|route2|route3}/', $controller)->setName('page');

Is there a more succinct/better/shorter way of writing that?

Try this:

    $app->get("/", function ($request, $response, $args) {
        $response->write("Home page");
        return $response;
    });

    $app->get("/{path:.*}", function ($request, $response, $args) {
        $path = $args['path'];
        $response->write("Page for: " . htmlentities($path));
        return $response;
    });
1 Like

Cool, thanks @akrabat, i really appreciate the help! :slight_smile: