Skip to content

Commit

Permalink
docs: update getting-stated
Browse files Browse the repository at this point in the history
  • Loading branch information
aristotelesbr committed Oct 9, 2024
1 parent db94e27 commit 5c317ad
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end
run app
```

## Use `Lennarb::Application::Base` class
## How to use `Lennarb::Application::Base` class

You can also use the `Lennarb::Application::Base` class to define your app:

Expand Down Expand Up @@ -116,6 +116,55 @@ After that, you can run your app with the `rackup` command:
$ rackup
```

## Mount route controllers

You can use the `mount` method to mount route controllers:

Create a route controller, in this example, we'll create a `UsersController`, and define your routes:

```ruby
# ../whatwever/users.rb

require 'lennarb'

class UsersController < Lennarb::Application::Base
get '/users' do |req, res|
res.html 'List of users'
end
end
```

Now, you can use the `mount` method to mount the `UsersController` on your app:

```ruby
# config.ru

require 'lennarb'
require_relative './whatwever/users'

class Application < Lennarb::Application::Base
mount UsersController
end
```

Completed! You can now execute your application using distinct controllers.

🚨 **IMPORTANT:** The `mount` method does not modify the hooks of the mounted controller. This means that the hooks of the mounted controller will not be executed in the context of the main application.

```ruby
# ../whatwever/users.rb

require 'lennarb'

class UsersController < Lennarb::Application::Base
before do |req, res|
puts 'UsersController before' # This will be executed in global context
end
end
```

We recommend you to use the `before` and `after` methods to define callbacks in the main application or specific routes. Ex. `before('/users')` will be executed only in the `UsersController` routes.

## Hooks

You can use the `before` and `after` methods to define callbacks:
Expand Down

0 comments on commit 5c317ad

Please sign in to comment.