Function registerFormInterface

Generates a form based interface to the given instance - scheduled to be deprecated in favor of vibe.web.web.registerWebInterface.

Each function is callable with either GET or POST using form encoded parameters. All methods of I that start with "get", "query", "add", "create", "post" are made available via the URL url_prefix~method_name. A method named "index" will be made available via url_prefix. method_name is generated from the original method name by the same rules as for vibe.http.rest.registerRestInterface. All these methods might take a HTTPServerRequest parameter and a HTTPServerResponse parameter, but don't have to.

All additional parameters will be filled with available form-data fields. Every parameter name has to match a form field name (or is a fillable struct). The registered handler will throw an exception if no overload is found that is compatible with all available form data fields.

If a parameter name is not found in the form data and the parameter is a struct, all accessible fields of the struct (might also be properties) will be searched in the form, with the parameter (struct) name prefixed. An underscore is used as delimiter. So if you have a struct parameter with name 'foo' of type:

struct FooBar {
	int bar;
	int another_foo;
}

the form data must contain the keys 'foo_bar' and 'foo_another_foo'. Their corresponding values will be applied to the structure's fields. If not all fields of the struct are found, this is considered an error and the next overload (if any) will be tried.

The registered handler gives really good error messages if no appropriate overload is found, but this comes at the price of some allocations for the error messages, which are not used at all if eventually a valid overload is found. So because of this and because the search for an appropriate overload is done at run time (according to the provided form data) you might want to avoid overloads for performance critical sites.

For a thorough example of how to use this method, see the form_interface example in the examples directory.

Prototype

void registerFormInterface(I)(
  URLRouter router,
  I instance,
  string url_prefix,
  MethodStyle style = MethodStyle.Unaltered,
  Flag!"strict" strict = Yes.strict
);

See Also

registerFormMethod, vibe.http.rest.registerRestInterface

Parameters

NameDescription
router The router the found methods are registered with.
instance The instance whose methods should be called via the registered URLs.
url_prefix The prefix before the method name. A method named getWelcomePage with a given url_prefix="/mywebapp/welcomePage/" would be made available as "/mywebapp/welcomePage/getWelcomePage" if MethodStyle is Unaltered.
style How the url part representing the method name should be altered.
strict Yes.strict if you want missing parameters in the form to be an error. No.strict if you are happy with the types' default value in this case. (If you have overloads this might cause not the best matching overload to be chosen.)

Examples

class FrontEnd {
	// GET /
	void index(HTTPServerResponse res)
	{
		res.render!("index.dt");
	}

	/// GET /files?folder=...
	void getFiles(HTTPServerRequest req, HTTPServerResponse res, string folder)
	{
		res.render!("files.dt", req, folder);
	}

	/// POST /login
	void postLogin(HTTPServerRequest req, HTTPServerResponse res, string username,
		string password)
	{
		if( username != "tester" || password != "secret" )
			throw new HTTPStatusException(HTTPStatus.Unauthorized);
		auto session = req.session;
		if( !session ) session = res.startSession();
		session["username"] = username;
		res.redirect("/");
	}
}

shared static this()
{
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	auto router = new URLRouter;
	registerFormInterface(router, new FrontEnd, "/");
	listenHTTP(settings, router);
}
Authors

Sönke Ludwig, Jan Krüger

Copyright

© 2012 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.