Search code examples
routescontent-management-systemslimslim-3

CMS like wildcard routing with Slim 3


I'm working on a (very lightweight) CMS on Slim 3 for some projects I’m working on. I’m struggling with the routing. Maybe someone here can nudge me in the right direction.

The admin is able to add new frontend pages in the backend. If added a page creates (automatically) a slug. There are a couple of pages (like the index page) that can't be deleted or edited (change the slug)

Now I have no idea how to set up the routing.

Example of what I want:

www.example.com/
www.example.com/contact.html www.example.com/some-page.html

I was able to set up a wildcard route but, to make things more interesting, I have some routes for the backend, too:

www.example.com/backend/
www.example.com/backend/dashboard/
$app->get('/[{path:.*}]', function($request, $response, $path = null) { return $response->write($path ? 'subroute' : 'index'); });

$app->group('', function () {
    $this->get('/backend/dashboard.html', 'BackendDashboardController:index')->setName('backend');
});

As soon as I implement those too I get the following error:

Static route "/backend/dashboard.html" is shadowed by previously defined variable route "/(.*)" for method "GET"

Every hint would be much appreciated.


Solution

  • It should work if you create group for the "/backend" path.

    $app->group('/backend', function () {
        $this->get('/dashboard.html', 'BackendDashboardController:index')->setName('backend');
    });
    
    $app->get('/[{path:.*}]', function($request, $response, $path = null) { return $response->write($path ? 'subroute' : 'index'); });