Project

General

Profile

Parameterized Routes/InternalPath with parameters

Added by Norbert Melzer about 11 years ago

Hi there!

Coming from Rails I am used to have URLs like "articles/1" for the article with ID 1. Is there a way to have similar behaviour with internalpathes?

Lets say I the URL is `example.com/app.wt/articles/1`, how can the internalpath-handler test for this style of internalpath and make an parameter for the actual function?

I have some twists with this internalpath things but I am required to write this app using C.

Hope you can help me

PS: This behaviour is called parameterized routes in Rails.


Replies (4)

RE: Parameterized Routes/InternalPath with parameters - Added by Norbert Melzer about 11 years ago

I know I could use the query-string as explained in https://mydailyhacks.wordpress.com/2012/09/22/wt-web-toolkit-get-url-parameter-string-values/, but I am not a friend of such approaches, I would prefer URLs that are "clean" and not to use query-strings.

RE: Parameterized Routes/InternalPath with parameters - Added by Marco M about 11 years ago

This is what I have right now. I am not completely satisfied, but it works.

void Foo::onInternalPathChange(const std::string& internalPath)
{
    if (internalPath.find("/articles/") == 0) {
        // Input validation.
        errno = 0;
        long id = strtol(internalPath.substr(10).c_str(), 0, 10);
        if (id <= 0 || errno) {
            Wt::log("info") << "path tampering or bug? " << internalPath;
        } else {
            showArticle(id);
        }
    }
}

RE: Parameterized Routes/InternalPath with parameters - Added by Koen Deforche about 11 years ago

Hey Marco,

That's the general idea. Also have a look at the example below, since there's a nextPart() utility function that will make your code more robust.

https://github.com/kdeforche/wt/blob/master/examples/blog/view/BlogView.C#L242

Regards,

koen

    (1-4/4)