diff --git a/README.md b/README.md index 175be40..2022c03 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,10 @@ Just like other things in `kemal`, browser redirection is super simple as well. # Redirect browser get "/logout" do |env| # important stuff like clearing session etc. - env.redirect "/login" # redirect to /login page + redirect "/login" # redirect to /login page end ``` +_Make sure to receive `env` as param in defined route's block or you might end-up having compile-time errors._ ## Middlewares diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index 368aa98..33b0c3a 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -187,7 +187,7 @@ describe "Kemal::Handler" do it "redirects user to provided url" do kemal = Kemal::Handler.new kemal.add_route "GET", "/" do |env| - env.redirect("/login") + redirect "/login" end request = HTTP::Request.new("GET", "/") response = kemal.call(request) diff --git a/src/kemal/context.cr b/src/kemal/context.cr index f1d83fd..173bd71 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -22,11 +22,6 @@ class Kemal::Context @response.content_type end - def redirect(url) - @response.headers.add "Location", url - @response.status_code = 301 - end - delegate headers, @request delegate status_code, @response delegate :"status_code=", @response diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr new file mode 100644 index 0000000..7a42b1e --- /dev/null +++ b/src/kemal/macros.cr @@ -0,0 +1,4 @@ +macro redirect(url) + env.response.headers.add "Location", {{url}} + env.response.status_code = 301 +end \ No newline at end of file