mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
better application handling, fix url_for method for routes with prefix
This commit is contained in:
@ -10,7 +10,11 @@ Also it stores application specific state that is shared across all handlers
|
||||
within same application.
|
||||
|
||||
Application acts as namespace for all routes, i.e all routes for specific application
|
||||
has same url path prefix:
|
||||
has same url path prefix. Application prefix always contains laading "/" slash.
|
||||
If supplied prefix does not contain leading slash, it get inserted.
|
||||
Prefix should consists of valud path segments. i.e for application with prefix `/app`
|
||||
any request with following paths `/app`, `/app/` or `/app/test` would match,
|
||||
but path `/application` would not match.
|
||||
|
||||
```rust,ignore
|
||||
# extern crate actix_web;
|
||||
@ -21,14 +25,14 @@ has same url path prefix:
|
||||
# }
|
||||
# fn main() {
|
||||
let app = Application::new()
|
||||
.prefix("/prefix")
|
||||
.prefix("/app")
|
||||
.resource("/index.html", |r| r.method(Method::GET).f(index))
|
||||
.finish()
|
||||
# }
|
||||
```
|
||||
|
||||
In this example application with `/prefix` prefix and `index.html` resource
|
||||
get created. This resource is available as on `/prefix/index.html` url.
|
||||
In this example application with `/app` prefix and `index.html` resource
|
||||
get created. This resource is available as on `/app/index.html` url.
|
||||
For more information check
|
||||
[*URL Matching*](./qs_5.html#using-a-application-prefix-to-compose-applications) section.
|
||||
|
||||
@ -56,6 +60,10 @@ fn main() {
|
||||
```
|
||||
|
||||
All `/app1` requests route to first application, `/app2` to second and then all other to third.
|
||||
Applications get matched based on registration order, if application with more general
|
||||
prefix is registered before less generic, that would effectively block less generic
|
||||
application to get matched. For example if *application* with prefix "/" get registered
|
||||
as first application, it would match all incoming requests.
|
||||
|
||||
## State
|
||||
|
||||
|
@ -445,7 +445,6 @@ fn main() {
|
||||
## Using a Application Prefix to Compose Applications
|
||||
|
||||
The `Applicaiton::prefix()`" method allows to set specific application prefix.
|
||||
If route_prefix is supplied to the include method, it must be a string.
|
||||
This prefix represents a resource prefix that will be prepended to all resource patterns added
|
||||
by the resource configuration. This can be used to help mount a set of routes at a different
|
||||
location than the included callable's author intended while still maintaining the same
|
||||
@ -471,7 +470,7 @@ fn main() {
|
||||
|
||||
In the above example, the *show_users* route will have an effective route pattern of
|
||||
*/users/show* instead of */show* because the application's prefix argument will be prepended
|
||||
to the pattern. The route will then only match if the URL path is /users/show,
|
||||
to the pattern. The route will then only match if the URL path is */users/show*,
|
||||
and when the `HttpRequest.url_for()` function is called with the route name show_users,
|
||||
it will generate a URL with that same path.
|
||||
|
||||
|
Reference in New Issue
Block a user