Merge pull request #32 from ilatif/master

Removed env from redirect and changed redirect from method to macro.
This commit is contained in:
Serdar Dogruyol 2015-12-09 22:27:04 +02:00
commit 12c6af7d79
4 changed files with 7 additions and 7 deletions

View file

@ -128,9 +128,10 @@ Just like other things in `kemal`, browser redirection is super simple as well.
# Redirect browser # Redirect browser
get "/logout" do |env| get "/logout" do |env|
# important stuff like clearing session etc. # important stuff like clearing session etc.
env.redirect "/login" # redirect to /login page redirect "/login" # redirect to /login page
end end
``` ```
_Make sure to receive `env` as param in defined route's block or you might end-up having compile-time errors._
## Middlewares ## Middlewares

View file

@ -187,7 +187,7 @@ describe "Kemal::Handler" do
it "redirects user to provided url" do it "redirects user to provided url" do
kemal = Kemal::Handler.new kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |env| kemal.add_route "GET", "/" do |env|
env.redirect("/login") redirect "/login"
end end
request = HTTP::Request.new("GET", "/") request = HTTP::Request.new("GET", "/")
response = kemal.call(request) response = kemal.call(request)

View file

@ -22,11 +22,6 @@ class Kemal::Context
@response.content_type @response.content_type
end end
def redirect(url)
@response.headers.add "Location", url
@response.status_code = 301
end
delegate headers, @request delegate headers, @request
delegate status_code, @response delegate status_code, @response
delegate :"status_code=", @response delegate :"status_code=", @response

4
src/kemal/macros.cr Normal file
View file

@ -0,0 +1,4 @@
macro redirect(url)
env.response.headers.add "Location", {{url}}
env.response.status_code = 301
end