I’m implementing a Pages system. Page slugs are immediately after the site root (
example.com/contact), and Page content is stored in the database.
I’d like to set up routing so that it first attempts to resolve a valid page, and if that fails, it goes on to the next defined route, the other controllers, etc.
Is there an established pattern for this type of setup? My goal is to to avoid manually specifying routes for every single page.
Showing first matched is default behaviour in CakePHP, but if page content is stored in database I recommend you create only one controller PagesController and action, which views page (for example display). Action will take content from database and show it. Then you can use this route.
$routes->connect( ‘/:slug’, [‘controller’ => ‘Articles’, ‘action’ => ‘display’] )
Method display($slug) will take one parameter containing given slug
I don´t think it´s good idea to show other page than user wants to. When page does´t exists (you ´ll find it inside display method in case you didn´t find given slug in database) you should throw NotFoundException.