Main  /  Edit version 1  /  Edit version 2  /   /  Users Area

Difference "Tutorial: URL Mapping (v 1.x)" ver. 1 versus ver. 2

Content:

<!--toc-->
## Introduction
Now we would learn how to connect between different URLs and the application functions easily.
When client requests specific URL from the HTTP server
it divides it into several parts: `SCRIPT_NAME`, `PATH_INFO` and `QUERY_STRING`. For example
URL `/foo/bar.php/test?x=10` is separated into:
- `SCRIPT_NAME` = `/foo/bar.php`
- `PATH_INFO` = `/test`
- `QUERY_STRING` = `x=10`
Today web applications are not distributed into several
scripts but rather written using a single FastCGI application that runs on specific url like "/myapp", So
generally URLs would look like `/myapp/page/10`, when
`SCRIPT_NAME` = "/myapp" remains constant and `PATH_INFO` = `/page/10` is changed according to user's needs.
So the mapping between URL and applications member functions
are done by matching a regular expression against `PATH_INFO` URL.
are done by matching a regular expression against `PATH_INFO` part of URL, even thou this behavior can be changed by using `mount_point` class.
## Example
Lets rewrite our [hello class](/wikipp/en/page/cppcms_1x_tut_hello):
First let's write our constructor:
hello(cppcms::service &srv) :
cppcms::application(srv)
{
dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
dispatcher().assign("/smile",&hello::smile,this);
dispatcher().assign("",&hello::welcome,this);
}
In the third line we connect the
In the fourth line we connect the regular expression `/number/(\d+)` to the member function `number` of `this`
instance that receives `std::string` as parameter,
and 1st captured subexpression is passed to it.
In this case the subexpression is nonempty string that contains digits.
In next two lines we connect member functions `smile` and `welcome` to the appropriate URLs.
So when the script name the HTTP server matches against is
`/hello` then for URLs
- `/hello` - `welcome` function would be called
- `/hello/smile` - `smile` function would be called
- `/hello/number/10` - `number` function would be called and receive a string "10" as parameter.
Now lets complete rest of parts of the code:
void number(std::string num)
{
int no = atoi(num.c_str());
response().out() << "The number is " << no << "\n";
}
void smile()
{
response().out() << ":-)";
}
void welcome()
{
response().out() <<
"<h1> Wellcome To Page with links </h1>\n"
"<a href=\"/hello/number/1\">1</a><br>\n"
"<a href=\"/hello/number/15\">15</a><br>\n"
"<a href=\"/hello/smile\">:-)</a><br>\n";
}
Start an application:
./hello -c config.js
And hit the URL: <http://localhost:8080/hello>

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page


Valid CSS | Valid XHTML 1.0