Redirecting a request
In a HTTP redirect, the client is sent a new URL to connect to in place of the one that it sent in the request. This might be used when a particular resource has been relocated to be accessible under a different URL.
In Woof!, the controller object provides the redirect method which sends a HTTP redirect response back to the client. Following are some examples of redirection assuming the original request was made to http://www.mybank.com/accounts/create.
Redirecting to a different action
method create {} { my redirect -action new }
Here, we treat create as a synonym for the new method, and simply redirect the browser accordingly. Note that the redirection defaults will result in generation of a URL that corresponds to the host and controller specified in the current request. Only the action method is changed.
Redirecting to a different controller
method create {} { if {![session exists username]} { my redirect -controller login return } ... create a new account .... }
In this modified example, the method checks if the user has logged on, and if not, redirects the browser to the login page. Unlike the previous example, we are now redirecting to a new controller, login. Moreover, since the -action option is not specified, the default action of the new controller is used.
Redirecting to a URL
method create {} { my redirect -url http://www.newbanklocation.com/createaccount }
In the previous examples, the redirection targets were within the same site. In this instance we redirect to an entirely new URL. No defaults will be picked up from the current request.
Redirecting with a non-default status and message
method create {} { my redirect -action new -httpstatus 301 -text "This page has been permanently moved." }
This example is the same as the first, except we explicitly specify the HTTP response code and text (which is not generally displayed in the browser).
Redirects and rendering
Note that because only one response can be sent back to the client, an application cannot redirect a request and simultaneously generate a Web page as a response. Therefore, using redirect together with either an implicit or explicit call to render is not permitted and will raise an error.