Method: Hanami::Slice::Router#slice

Defined in:
lib/hanami/slice/router.rb

#slice(slice_name, at:, as: nil, &blk) ⇒ Object

Yields a block for routes to resolve their action components from the given slice.

An optional URL prefix may be supplied with at:.

Examples:

# config/routes.rb

module MyApp
  class Routes < Hanami::Routes
    slice :admin, at: "/admin" do
      # Will route to the "actions.posts.index" component in Admin::Slice
      get "posts", to: "posts.index"
    end
  end
end

Parameters:

  • slice_name (Symbol)

    the slice’s name

  • at (String, nil)

    optional URL prefix for the routes

Since:

  • 2.0.0



74
75
76
77
78
79
80
81
82
83
# File 'lib/hanami/slice/router.rb', line 74

def slice(slice_name, at:, as: nil, &blk)
  blk ||= @resolver.find_slice(slice_name).routes

  prev_resolver = @resolver
  @resolver = @resolver.to_slice(slice_name)

  scope(at, as:, &blk)
ensure
  @resolver = prev_resolver
end