create-slice
Use this skill when creating and configuring Hanami 2.x Slices.
Quick Reference
| Scenario | Approach |
|---|---|
| Create a Slice | hanami generate slice <name> |
| Register a Slice | Add to config/app.rb with slice :name, at: "/path" |
| Define Slice routes | Create slices/<name>/config/routes.rb |
| Access Slice Actions | Routes point to slice_name.action_name |
| Import from another Slice | Use import in the Slice class or app config |
| Export from a Slice | Use export in the Slice class |
| Slice-level container | MyApp::Slices::Api::Container |
| Access Slice components | include Deps["slices.api.repositories.users"] |
Core Rules
Generate a Slice using the Hanami CLI:
hanami generate slice apiCreates
slices/api/with subdirectories includingconfig/routes.rb,actions/,views/, andtemplates/.Verify: Run
hanami routesand confirm the new slice's routes appear in the output.Register the Slice in the app:
# config/app.rb module MyApp class App < Hanami::App slice :api, at: "/api" do # Slice-specific configuration end end endDefine Slice routes in
slices/<name>/config/routes.rb:# slices/api/config/routes.rb module MyApp module Slices module Api class Routes < Hanami::Routes get "/users", to: "users.index" get "/users/:id", to: "users.show" end end end endRoutes map to action files under
slices/api/actions/(e.g./api/users→slices/api/actions/users/index.rb).Verify: Run
hanami routesand confirm/api/usersand/api/users/:idare listed.Import dependencies from another Slice:
# config/app.rb module MyApp class App < Hanami::App slice :api, at: "/api" do import from: :main do # Import specific components from the main slice end end end endVerify: Boot the app (
hanami console) and resolve the imported component:MyApp::Slices::Api::Container["main.repositories.users"].Export Slice components for use by other slices:
# slices/api/config/slice.rb module MyApp module Slices module Api class Slice < Hanami::Slice export ["repositories.users"] end end end endVerify: In
hanami console, confirm the consuming slice can resolve the exported key.Keep Slices self-contained and minimize cross-slice dependencies. Use
import/exportas the formal contract between slices.Use Slices for bounded contexts — each slice encapsulates a distinct domain concern such as a public API, admin dashboard, billing, or main web application.
Common Mistakes
- Circular slice dependencies: Slices should be acyclic. If Slice A imports from Slice B, Slice B must not import from Slice A.
- Direct container access across slices: Use
import/exportfor cross-slice communication. Never accessHanami.app["slices.other.component"]directly. - Misaligned namespaces: Slice components live under
MyApp::Slices::Api::. File paths and module namespaces must align.
Integration
| Related Skill | When to chain |
|---|---|
| define-routes | Slices define their own routes. Master routes before creating slices. |
| inject-dependencies | Cross-slice dependencies use import/export and are injected via Deps. |
| configure-slice | Configure slice-level settings and providers after creating the slice. |
| create-new-slice (workflow) | Full workflow for creating and configuring a new slice. |