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

Difference "cppcms::url_parser" ver. 1 versus ver. 2

Content:

## Role
This class is used for spawning requests according given URL by matching them by regular expressions.
It is defined in `cppcms/url.h` header.
## Constructor
url_parser();
url_parser(worker_thread *w);
Creates new URL parser. First version creates standalone parser that can be used for any input string. Second derives the string from CGI variable `PATH_INFO`.
## General Recommendations
Always use callbacks to setup your url dispatching functions.
## Public Member Functions
### Setting Handlers
#### Binding to callback
void add(char const *expr,callbac_t );
Binds expression to callback. `callback_t` is boost::function with 9 string parameters where:
- First 8 parameters are matched expressions --- $1,..,$8
in "perl" semantics.
- Last 9 parameter is entry expression -- $0 in "perl" semantics.
For example:
class app : public application {
void page(string id);
...
app(...) ...
{
url.add("^/page/(\\d+)$",
boost::bind(&app::page,this,_1));
}
};
Would setup callback to `page` member function and pass string id with matched number.
In order to remember in more easy way these parameters, macros $0, $1, ... $8 are defined in `url.h` header. Thus you can rewrite above lines as:
url.add("^/page/(\\d+)$",
boost::bind(&app::page,this,$1));
In versions prior to CppCMS 0.0.4, you could use macros $0, $1, ... $8 that were defined in `url.h` header. They are
depricated and should not be used becuase `$` in identifiers
is not supported on some platforms like ARM.
#### Binding to ID
void add(char const *exp,int id);
Causes `url_parser::parse` return `id` when match had found.
#### Binding to other url\_parser:
void add(char const *exp,url_parser &url);
Forces `url_parser` to pass string to other parser if match was found. It may be used for faster matching urls. For example:
admin_url.add("^/admin/add_user$",...);
admin_url.add("^/admin/remove_user$",...);
...
app_url.add("^/app/article/(\\d+)$",...);
...
url.add("^/admin/.*",admin_url);
url.add("^/app/.*",app_url);
### Parsing URL
int parse();
int parse(string &s);
Parses URL using `s` or `PATH_INFO`. It returns
- `url_parser::ok` if callback was dispatched
- `url_parser::not_found` if no match was found.
- Other positive number if URL was associated with number and not callback.
If you using `url_parser` that returns id, you can find matched parameters using operator `[]`. For example:
url.add("^/page/(\\d+)$",1);
switch(url.parse()) {
case 1: id=url[1];
}

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