Skip to content

Commit 64d4d8c

Browse files
committed
feat: update named route docs
1 parent 0f50bad commit 64d4d8c

File tree

2 files changed

+71
-38
lines changed

2 files changed

+71
-38
lines changed

src/docs/database/index.md

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,45 @@ db()->connect([
6666
]);
6767
```
6868

69-
The `connect()` method takes an array of connection details for your database as its argument. Depending on the database system you're using, you'll need to provide different connection details. For example, here's how you can connect to a SQLite database:
69+
The `connect()` method takes an array of connection details for your database as its argument. Depending on the database system you're using, you'll need to provide different connection details.
70+
Here are some examples of how you can connect to different databases:
7071

71-
```php
72+
::: code-group
73+
74+
```php [MySQL]
75+
db()->connect([
76+
'host' => '127.0.0.1',
77+
'username' => 'root',
78+
'password' => '',
79+
'dbname' => 'Leaf',
80+
]);
81+
```
82+
83+
```php [PostgreSQL]
84+
db()->connect([
85+
'dbtype' => 'pgsql',
86+
'host' => '127.0.0.1',
87+
'username' => 'root',
88+
'password' => '',
89+
'dbname' => 'Leaf',
90+
'port' => '5432',
91+
]);
92+
```
93+
94+
```php [SQLite]
7295
db()->connect([
7396
'dbtype' => 'sqlite',
7497
'dbname' => 'db.sqlite',
7598
]);
7699
```
77100

78-
If you have a environment file, you can use it to store your database connection details. Here's an example of how you can connect to a MySQL database using an environment file:
101+
:::
102+
103+
If you are using Leaf MVC, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details.
104+
105+
::: code-group
79106

80-
```txt
107+
```txt [MySQL]
81108
DB_CONNECTION=mysql
82109
DB_HOST=127.0.0.1
83110
DB_PORT=3306
@@ -86,12 +113,24 @@ DB_USERNAME=root
86113
DB_PASSWORD=
87114
```
88115

89-
Using the environment file, you can connect to the database like this:
116+
```txt [PostgreSQL]
117+
DB_CONNECTION=pgsql
118+
DB_HOST=127.0.0.1
119+
DB_PORT=5432
120+
DB_DATABASE=LeafMVC
121+
DB_USERNAME=root
122+
DB_PASSWORD=
123+
```
90124

91-
```php
92-
db()->autoConnect();
125+
```txt [SQLite]
126+
DB_CONNECTION=sqlite
127+
DB_DATABASE=/absolute/path/to/database.sqlite
93128
```
94129

130+
:::
131+
132+
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
133+
95134
## Writing simple queries
96135

97136
Once you've connected to a database, you can start writing queries to interact with it. Queries are the commands you run on your database to get, insert, update or delete data. Leaf DB provides a simple way to run queries using the query builder, but also allows you to run raw SQL queries.
@@ -147,22 +186,3 @@ There may be times when you want to get a single value from a query that returns
147186
```php
148187
$user = db()->query('SELECT * FROM users')->first();
149188
```
150-
151-
## Leaf DB + MVC
152-
153-
Leaf MVC comes with a more structured way to work with databases. You can use models to interact with your database. This makes it easier to manage your database operations and keep your code clean. However, you can still use the query builder to run raw queries if you need to.
154-
155-
To get started, you need to set up your database connection in your environment file.
156-
157-
```txt
158-
DB_CONNECTION=mysql
159-
DB_HOST=127.0.0.1
160-
DB_PORT=3306
161-
DB_DATABASE=LeafMVC
162-
DB_USERNAME=root
163-
DB_PASSWORD=
164-
```
165-
166-
From there, you just need to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
167-
168-
If you have multiple databases, Leaf DB will connect to your database which has been labelled as your default database. Leaf DB does not yet support multiple database connections, but of course, you can manually create multiple instances of Leaf DB.

src/docs/routing/index.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ app()->get('/home', ['name' => 'home', function () {
110110
}]);
111111
```
112112

113+
You can then redirect to this route using the route name by passing an array with the route name to the `redirect()` method.
114+
115+
```php:no-line-numbers
116+
response()->redirect(['home']);
117+
```
118+
119+
If you want to get details about a route, you can use the `getRoute()` method.
120+
121+
```php:no-line-numbers
122+
$route = app()->route($routeName);
123+
```
124+
125+
This will return an array containing the following information:
126+
127+
- `pattern`: The route pattern
128+
- `path`: The route path
129+
- `name`: The route name
130+
- `method`: The route method
131+
- `handler`: The route handler
132+
- Any other route options
133+
113134
## Getting the current route
114135

115136
There are times when you need to get the current route which the user is visiting from inside your route handler. You can do this by calling the `getRoute()` method on the router instance.
@@ -132,24 +153,16 @@ This method returns an array containing the following information:
132153

133154
## Navigating to another route
134155

135-
There are times when you need to redirect users to another route. For example, after a user logs in, you might want to redirect them to their dashboard. You can do this by calling the `push()` method on the router instance or the `redirect()` method on the response instance.
156+
There are times when you need to redirect users to another route. For example, after a user logs in, you might want to redirect them to their dashboard. You can do this by calling the `redirect()` method on the response instance.
136157

137-
::: code-group
138-
139-
```php:no-line-numbers [router]
140-
app()->push('/login');
141-
```
142-
143-
```php:no-line-numbers [response]
158+
```php:no-line-numbers
144159
response()->redirect('/login');
145160
```
146161

147-
:::
148-
149-
If your route has a name, you can navigate to it by passing the route name in an array to the `push()` method.
162+
If your route has a name, you can navigate to it by passing the route name in an array to the `redirect()` method.
150163

151164
```php:no-line-numbers
152-
app()->push(['home']);
165+
response()->redirect(['home']);
153166
```
154167

155168
## Routing in Leaf MVC

0 commit comments

Comments
 (0)