Right, sorry I didn’t realise how much that lesson relies on
PageController - but you should be able to take the overall lessons from that and use a new controller (subclass either
ContentController or just directly
Controller) and use routing to define the URL handler for that controller.
So in your example, you would have something like this
In this case the
MyController class will be given any URLs which start with
/visit, which will map to its
index() method, with
/visit/something mapping to a
something action if you defined such an action
example.com/listing/1 map to an object with the ID 1 you would need your rule to look like this:
listing//$ID which I think would work - but you might have to experiment a bit. You would then need to dictate a
url_handlers array in your controller class that has something like:
private static $url_handlers = [
'listing//$ID' => 'index',
and then in the index method of the controller, you can check the ID, return a 404 if it doesn’t exist, or return the rendered template if it does.
Note also that using IDs directly on the front-end like that is a bit of an anti-pattern. You might want to look at the
getUniqueKey() method on
DataObject and use that instead of the database ID.