From 0a166cd5937884ec848ce034aa725fe895c035aa Mon Sep 17 00:00:00 2001 From: dowwie Date: Wed, 23 May 2018 15:46:14 -0400 Subject: [PATCH 1/3] added section on Scopes, within url dispatch --- content/docs/url-dispatch.md | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/content/docs/url-dispatch.md b/content/docs/url-dispatch.md index ac300b6..1b48f3e 100644 --- a/content/docs/url-dispatch.md +++ b/content/docs/url-dispatch.md @@ -285,6 +285,57 @@ foo/1/2/ -> Params{'bar':'1', 'tail': '2/'} foo/abc/def/a/b/c -> Params{'bar':u'abc', 'tail': 'def/a/b/c'} ``` +# Scoping Routes + +Scoping helps you organize routes sharing common root paths. You can nest +scopes within scopes. + +Suppose that you want to organize paths to endpoints used to manage a "Project", +consisting of "Tasks". Such paths may include: + +- /project +- /project/{project_id} +- /project/{project_id}/task +- /project/{project_id}/task/{task_id} + + +A scoped layout of these paths would appear as follows + +```rust + App::new() + .scope("/project", |proj_scope| { + proj_scope + .resource("/", |r| { + r.method(Method::GET) + .f(get_projects); + r.method(Method::POST) + .f(create_project)}) + .resource("/{project_id}", |r| { + r.method(Method::PUT) + .with(update_project); + r.method(Method::DELETE) + .f(delete_project)}) + .nested("/{project_id}/task", |task_scope| { + task_scope + .resource("/", |r| { + r.method(Method::GET) + .f(get_tasks); + r.method(Method::POST) + .f(create_task)}) + .resource("/{task_id}", |r| { + r.method(Method::PUT) + .with(update_task); + r.method(Method::DELETE) + .with(delete_task)})})}) +``` + +A *scoped" path can contain variable path segments as resources. Consistent with +unscoped paths, a scoped prefix without a trailing slash has one automatically +appended to it: `/app` converts to `/app/`. + + You can get variable path segments from `HttpRequest::match_info()`. + `Path` extractor also is able to extract scope level variable segments. + # Match information All values representing matched path segments are available in From c98a067163d1509dbe303c72c42f82074d98af83 Mon Sep 17 00:00:00 2001 From: dowwie Date: Wed, 23 May 2018 15:48:50 -0400 Subject: [PATCH 2/3] typo --- content/docs/url-dispatch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/url-dispatch.md b/content/docs/url-dispatch.md index 1b48f3e..df9ae7e 100644 --- a/content/docs/url-dispatch.md +++ b/content/docs/url-dispatch.md @@ -329,7 +329,7 @@ A scoped layout of these paths would appear as follows .with(delete_task)})})}) ``` -A *scoped" path can contain variable path segments as resources. Consistent with +A *scoped* path can contain variable path segments as resources. Consistent with unscoped paths, a scoped prefix without a trailing slash has one automatically appended to it: `/app` converts to `/app/`. From 8ba23ce8148aa17adf854975126c7a9476a8107d Mon Sep 17 00:00:00 2001 From: dowwie Date: Thu, 24 May 2018 10:49:13 -0400 Subject: [PATCH 3/3] removed slash from resources in scope example --- content/docs/url-dispatch.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/url-dispatch.md b/content/docs/url-dispatch.md index df9ae7e..aaf5508 100644 --- a/content/docs/url-dispatch.md +++ b/content/docs/url-dispatch.md @@ -305,7 +305,7 @@ A scoped layout of these paths would appear as follows App::new() .scope("/project", |proj_scope| { proj_scope - .resource("/", |r| { + .resource("", |r| { r.method(Method::GET) .f(get_projects); r.method(Method::POST) @@ -317,7 +317,7 @@ A scoped layout of these paths would appear as follows .f(delete_project)}) .nested("/{project_id}/task", |task_scope| { task_scope - .resource("/", |r| { + .resource("", |r| { r.method(Method::GET) .f(get_tasks); r.method(Method::POST)