Skip to content

Commit 03b3e02

Browse files
committed
feat: update with new mvc version docs
1 parent 7e50db4 commit 03b3e02

File tree

15 files changed

+262
-112
lines changed

15 files changed

+262
-112
lines changed

.vitepress/config/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const sidebar = [
161161
{ text: 'Factories', link: '/docs/database/factories' },
162162
{ text: 'Writing Commands', link: '/docs/mvc/commands' },
163163
// { text: 'Mailing', link: '/docs/utils/mail/mvc' },
164-
{ text: 'MVC Helpers', link: '/docs/mvc/globals' },
164+
{ text: 'MVC Globals', link: '/docs/mvc/globals' },
165165
{ text: 'Custom Libraries', link: '/docs/mvc/libraries' },
166166
{ text: 'MVC Console Tool', link: '/docs/mvc/console' },
167167
],

.vitepress/theme/components/shared/Banner.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const dismiss = () => {
3131
<template>
3232
<div ref="el" class="banner">
3333
<div class="text">
34-
WARNING You're browsing the documentation for an upcoming version of Leaf. The documentation and features of this release are subject to change.
34+
🎉 Leaf MVC v3.8 released. Check out the <a href="https://blog.leafphp.dev/posts/leaf-mvc-3-8" target="_blank">release post</a>
3535
</div>
3636

3737
<button type="button" @click="dismiss">

.vitepress/theme/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { h } from 'vue';
1+
import { defineAsyncComponent, h } from 'vue';
22
import DefaultTheme from 'vitepress/theme';
33
import { VueWriter } from 'vue-writer';
44
import { MotionPlugin } from '@vueuse/motion';
@@ -15,8 +15,8 @@ export default {
1515
},
1616
Layout() {
1717
return h(DefaultTheme.Layout, null, {
18-
// 'layout-top': () =>
19-
// h(defineAsyncComponent(() => import('./components/shared/Banner.vue'))),
18+
'layout-top': () =>
19+
h(defineAsyncComponent(() => import('./components/shared/Banner.vue'))),
2020
});
2121
},
2222
};

src/docs/database/factories.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Factories are a way to conveniently generate large amounts of database records.
1010

1111
To create a factory, you can use the `g:factory` command. This command will create a new factory class in the `app/database/factories` directory.
1212

13-
```bash
13+
```bash:no-line-numbers
1414
php leaf g:factory UserFactory
1515
```
1616

@@ -58,7 +58,7 @@ This factory class defines the attributes for the `User` model. The `definition`
5858

5959
To use a factory to generate records, you can use the `create` method on the factory class. This method will create a new record in the database using the attributes defined in the factory.
6060

61-
```php
61+
```php:no-line-numbers
6262
(new UserFactory)->create(20)->save();
6363
```
6464

@@ -92,14 +92,14 @@ This will generate 20 records in the `users` table using the attributes defined
9292

9393
In some cases, you may not want to save the generated records to the database. You can use the `get()` method to get the generated data without saving it to the database.
9494

95-
```php
95+
```php:no-line-numbers
9696
$users = (new UserFactory)->create(20)->get();
9797
```
9898

9999
## Running Factories
100100

101101
You don't need to run factories manually. You can use the `db:seed` command to run your seeders, which will in turn run your factories.
102102

103-
```bash
103+
```bash:no-line-numbers
104104
php leaf db:seed
105105
```

src/docs/database/index.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ Databases are essential for most applications, as they help you store and retrie
4949

5050
:::
5151

52+
## Leaf MVC + DB
53+
54+
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need for this module unless you want to use Leaf Auth. If you choose to use Leaf DB in your MVC application, 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. Here are a few example connections:
55+
56+
::: code-group
57+
58+
```txt [MySQL]
59+
DB_CONNECTION=mysql
60+
DB_HOST=127.0.0.1
61+
DB_PORT=3306
62+
DB_DATABASE=LeafMVC
63+
DB_USERNAME=root
64+
DB_PASSWORD=
65+
```
66+
67+
```txt [PostgreSQL]
68+
DB_CONNECTION=pgsql
69+
DB_HOST=127.0.0.1
70+
DB_PORT=5432
71+
DB_DATABASE=LeafMVC
72+
DB_USERNAME=root
73+
DB_PASSWORD=
74+
```
75+
76+
```txt [SQLite]
77+
DB_CONNECTION=sqlite
78+
DB_DATABASE=/absolute/path/to/database.sqlite
79+
```
80+
81+
:::
82+
83+
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.
84+
85+
You can safely skip the "Connecting to a database" section.
86+
5287
## Connecting to a database
5388

5489
The first step to using a database is to create a connection. It's like opening a door to the database, allowing you to interact with it. Here's how you can connect to a database using Leaf:
@@ -100,44 +135,13 @@ db()->connect([
100135

101136
:::
102137

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
106-
107-
```txt [MySQL]
108-
DB_CONNECTION=mysql
109-
DB_HOST=127.0.0.1
110-
DB_PORT=3306
111-
DB_DATABASE=LeafMVC
112-
DB_USERNAME=root
113-
DB_PASSWORD=
114-
```
115-
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-
```
124-
125-
```txt [SQLite]
126-
DB_CONNECTION=sqlite
127-
DB_DATABASE=/absolute/path/to/database.sqlite
128-
```
129-
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-
134138
## Writing simple queries
135139

136140
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.
137141

138142
We can run queries using the `query()` method. This method takes in a query string and returns a query builder instance. This means that you can run queries like this:
139143

140-
```php
144+
```php:no-line-numbers
141145
$users = db()->query('SELECT * FROM users')->all();
142146
```
143147

@@ -158,13 +162,13 @@ There are different kinds of database commands: some give you results (like data
158162

159163
You can use `execute()` to run queries that don't return values. This method returns `true` if the query was successful and `false` if it wasn't. You can run a query like this:
160164

161-
```php
165+
```php:no-line-numbers
162166
db()->query('CREATE DATABASE dbname')->execute();
163167
```
164168

165169
If you want to run a query that returns data, you can use the `all()` method to get all the results. For example, you can run a query like this:
166170

167-
```php
171+
```php:no-line-numbers
168172
$users = db()->query('SELECT * FROM users')->all();
169173
```
170174

@@ -183,6 +187,6 @@ This will return the matched user as an object.
183187

184188
There may be times when you want to get a single value from a query that returns multiple rows. In such cases, you can use the `first()` method. For example, you can run a query like this:
185189

186-
```php
190+
```php:no-line-numbers
187191
$user = db()->query('SELECT * FROM users')->first();
188192
```

src/docs/database/models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This code will retrieve all rows from the `flights` table and then loop through
5151

5252
The Leaf all method will return all of the results in the model's table. Since each Leaf model serves as a query builder, you may also add constraints to queries, and then use the get method to retrieve the results:
5353

54-
```php
54+
```php:no-line-numbers
5555
$flights = Flight::where('active', 1)->orderBy('name', 'desc')->take(10)->get();
5656
```
5757

src/docs/http/cors.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ app()->cors([
7979

8080
This will allow `http://example.com`, `https://example.com`, `http://www.example.com`, and `https://some-subdomain.example.com` to access your app. Of course, you can also use a regular expression to match multiple domains. You can find a full list of options below.
8181

82+
## CORS + Leaf MVC
83+
84+
If you are using Leaf MVC, you can configure CORS using your environment variables in place of the configuration above:
85+
86+
```txt:no-line-numbers [.env]
87+
CORS_ALLOWED_ORIGINS='/\.example\.com$/'
88+
CORS_ALLOWED_METHODS='GET,HEAD,PUT,PATCH,POST,DELETE'
89+
CORS_ALLOWED_HEADERS='*'
90+
```
91+
92+
While this is easier and allows you to easily configure different environments, it can sometimes be limiting for example when you want to return a function for dynamically set your allowed origins. For this reason, you can publish your CORS configuration using the command below:
93+
94+
```bash:no-line-numbers
95+
php leaf config:publish cors
96+
```
97+
98+
This will create or update your CORS config in `config/cors.php`. You can then use the options below to configure the CORS module to suit your exact needs.
99+
82100
## Configuration Options
83101

84102
The `cors()` method takes in an array of options. Here are the available options:

src/docs/http/headers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Leaf\Http\Headers::remove(['WWW-Authenticate', 'Authorization']);
122122

123123
You can check if a header exists using the `Leaf\Http\Headers::has()` method. This method takes in a single parameter, the header to check and returns a boolean.
124124

125-
```php
125+
```php:no-line-numbers
126126
$exists = Leaf\Http\Headers::has('Content-Type');
127127
```
128128

src/docs/mvc/commands.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CachePurgeCommand extends Command
8282
}
8383
```
8484

85-
If you are using Leaf MVC v3.8 and above, the command is automatically loaded by Leaf MVC. If you are using Leaf MVC v3.7 and below, you will need to register the command in the `app/console/Commands.php` file.n
85+
If you are using Leaf MVC v3.8 and above, the command is automatically loaded by Leaf MVC. If you are using Leaf MVC v3.7 and below, you will need to register the command in the `app/console/Commands.php` file.
8686

8787
```php
8888
<?php
@@ -112,7 +112,7 @@ class Commands
112112

113113
Command arguments are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this:
114114

115-
```bash
115+
```bash:no-line-numbers
116116
php leaf example argument
117117
```
118118

@@ -138,7 +138,7 @@ protected function handle()
138138

139139
Command options are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this:
140140

141-
```bash
141+
```bash:no-line-numbers
142142
php leaf example --option=value
143143
```
144144

@@ -371,6 +371,6 @@ This method asks a question but hides the keystrokes. It takes in 2 parameters:
371371
- the question to ask
372372
- use hidden fallback (optional)
373373

374-
```php
374+
```php:no-line-numbers
375375
$password = $this->secret('Confirm your password');
376376
```

src/docs/mvc/console.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ Leaf MVC includes a powerful command-line tool called Aloe to help you manage yo
88
php leaf list
99
```
1010

11+
::: details Missing commands?
12+
13+
If you get errors from commands which you saw in the documentation, you are probably running an older version of the Leaf MVC console. We add more handy commands regularly, but as the console does not automatically update, you may run into the missing command error. To fix that problem, you need to install the latest version of Aloe:
14+
15+
```bash:no-line-numbers
16+
leaf install aloe
17+
18+
# or with composer
19+
20+
composer require leafs/aloe
21+
```
22+
23+
:::
24+
1125
## Aloe vs. Leaf CLI: What's the Difference?
1226

1327
Before diving in, it’s important to know that Aloe is different from Leaf CLI.

0 commit comments

Comments
 (0)