Method: Hanami::Router#redirect

Defined in:
lib/hanami/router.rb

#redirect(path, options = {}, &endpoint) ⇒ Hanami::Routing::Route

Defines an HTTP redirect

Examples:

require 'hanami/router'

Hanami::Router.new do
  redirect '/legacy',  to: '/new_endpoint'
  redirect '/legacy2', to: '/new_endpoint2', code: 302
end
require 'hanami/router'

router = Hanami::Router.new
router.redirect '/legacy',  to: '/new_endpoint'

Parameters:

  • path (String)

    the path that needs to be redirected

  • options (Hash) (defaults to: {})

    the options to customize the redirect behavior

Options Hash (options):

  • the (Fixnum)

    HTTP status to return (defaults to ‘301`)

Returns:

  • (Hanami::Routing::Route)

    the generated route. This may vary according to the ‘:route` option passed to the initializer

See Also:

Since:

  • 0.1.0



615
616
617
# File 'lib/hanami/router.rb', line 615

def redirect(path, options = {}, &endpoint)
  get(path).redirect @router.find(options), options[:code] || 301
end