I have a virtual function that is called handlePathChange() in my Controller class.
It checks the current URL and should dispatch the right view for it.
Here's the code I have so far:
void Controller::handlePathChange()
{
if ( app->internalPathMatches(basePath) )
{
string path = app->internalPathNextPart(basePath);
if ( path.empty() ) // If it's empty it is known that the index of the controller should show up
index();
// else if ( path == ?? ) each controller has it's own routes
// call_some_unknown_function();
}
}
How can I generalize this?
I was thinking about two options:
- Call a pure virtual function called dispatch() that will match the right path to the right function in the derived class. This solution violates DRY as basically you will write the same code over and over again.
- Create a hash maps of std::function but then if a part of the url is a parameter then the view won't be found. So that option isn't good enough.
Any ideas?