Skip to content

Commit aacb53e

Browse files
committed
feat: complete /docs section
1 parent 7f08538 commit aacb53e

File tree

19 files changed

+583
-482
lines changed

19 files changed

+583
-482
lines changed

.vitepress/config/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const sidebar = [
5151
// { text: 'Overview', link: '/docs/config/' },
5252
// { text: 'App settings', link: '/docs/config/settings' },
5353
{ text: 'Application Env', link: '/docs/config/environment' },
54-
{ text: 'URL Rewriting', link: '/docs/routing/url-rewriting' },
54+
{ text: 'Deployment', link: '/learn/deployment/' },
5555
{ text: 'Error Handling', link: '/docs/routing/error-handling' },
5656
{ text: 'Dependency Injection', link: '/docs/config/container' },
5757
// { text: 'Logging', link: '/docs/utils/logging' },

src/docs/database/files.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Schema Files <Badge type="warning">ALPHA v4+</Badge>
22

3-
Leaf MVC inherited all the teachings of Laravel and Ruby on Rails, including the use of migrations, seeders, and factories which made creating, testing and seeding databases a breeze. While this is great and has been tried and tested over the years, having multiple files for a single database table can be a bit of a hassle. This is why we introduced Schema Files in Leaf MVC v4.
3+
Leaf MVC took inspiration from Laravel and Ruby on Rails, adopting migrations, seeders, and factories to make database management easy. While this approach is powerful, managing multiple files for a single table can become a hassle.
4+
5+
That’s why in Leaf MVC v4, we’re introducing Schema Files—a simpler, more intuitive way to define, test, and seed your database in one place.
46

57
## What are Schema Files?
68

7-
Schema files build on the JSON schema idea we introduced in earlier Leaf MVC versions, but they take things further. Instead of juggling separate files for migrations, seeders, and factories, you can handle everything in one place. They’re written in YAML, so they’re easy to read and work with—no extra hassle, no repeating yourself.
9+
Schema Files build on the JSON schema idea from earlier Leaf MVC versions but take things even further. Instead of managing separate migrations, seeders, and factories, everything is now in one place. Written in YAML, they’re clean, easy to read, and eliminate unnecessary repetition—so you can focus on building, not juggling files.
810

911
```yml [flights.yml]
1012
columns:
@@ -175,17 +177,15 @@ php leaf db:script ImportUsersFromOldTable
175177

176178
## Migration histories
177179

178-
Migration histories are used to keep track of the migrations that have been run on your database. This is useful for keeping track of the state of your database so you can easily roll back to a previous state if needed. Unlike in other frameworks, Leaf MVC does require you to manually create migrations to keep track of your migration history. This is done automatically for you.
179-
180-
Every time you edit a schema file and run the `db:migrate` command, Leaf will automatically keep track of the migrations that have been run on your database, which means less time scrambling through migration files and more time building your app.
180+
Migration histories keep track of changes to your database, making it easy to roll back if needed. Unlike other frameworks, **Leaf MVC handles this automatically**—no need to manually create migrations just to track history.
181181

182-
In the end, this means you can continue to use `php leaf db:rollback` to roll back your database to a previous state.
182+
Every time you update a Schema File and run `db:migrate`, Leaf logs the changes for you. No more digging through migration files—just focus on building. And when you need to roll back? Simply run `php leaf db:rollback`.
183183

184184
## Seeding your database
185185

186-
Database seeds are a way to populate a database with initial data. This initial data can be used to set up default values or pre-populate a database with test data. Database seeds typically contain small amounts of data, such as default settings, test data, or sample records.
186+
Database seeds let you pre-populate your database with initial data—whether it's default settings, test data, or sample records. Instead of manually adding entries, you can use seeders to automate this process.
187187

188-
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeds` key in your database file is used to create seeders. Here's an example of a seeder:
188+
In Leaf MVC, you define seeders directly in your Schema Files under the `seeds` key. This keeps everything in one place, making it easier to manage your database setup. Here's an example of a seeder:
189189

190190
```yml [users.yml]
191191
seeds:

src/docs/database/models.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Models
22

3-
A model is a class that represents your app's data. It acts like the middle-man between your database and your app, allowing you work with the data in your database without writing complicated database code. Models have some advantages over using raw SQL queries and even using a query builder like Leaf DB, some of these advantages include:
3+
A model is a class that represents your apps data, acting as a bridge between your database and application. Instead of [building complex SQL queries](/docs/database/builder), models let you work with your data in a clean, reusable, and organized way.
44

5-
- Organization: Models keep your code organized by separating the database logic from other parts of your application (like views or controllers). This makes your code cleaner and easier to maintain.
5+
## Why use models?
66

7-
- Reusability: Once a model is set up, you can reuse it throughout your app to handle database interactions without duplicating code.
7+
- Organization – Keeps database logic separate from your views and controllers, making your app cleaner and easier to maintain.
8+
- Reusability – Define once, use anywhere—no need to repeat database code.
9+
- Consistency – Enforces a structured way of interacting with data, reducing errors.
810

9-
- Consistency: Models enforce a consistent way of working with your data, reducing the chances of errors when interacting with the database.
10-
11-
On top of all that, a single model corresponds to a single database table. This makes your data more organized and easier to work with.
11+
Each model maps to a database table, keeping your data structured and easy to manage.
1212

1313
## Creating a model
1414

@@ -139,7 +139,7 @@ $flight->delete(); // Delete the flight
139139

140140
A soft delete marks a record as deleted without actually removing it from the database. Instead, a `deleted_at` timestamp is set, and the record is hidden from query results. The data is still in the database, allowing you to restore it later if needed. To get started with soft deletes, add a `deleted_at` column to your table. You can do this by adding the following line to your [migration file](/docs/database/migrations):
141141

142-
```php
142+
```php:no-line-numbers
143143
$table->timestamp('deleted_at')->nullable();
144144
```
145145

src/docs/database/redis.md

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,7 @@ composer require leafs/redis
7070

7171
:::
7272

73-
Leaf Redis comes with a few commands that you can attach to the MVC console. You can do this by heading over to the `leaf` file in your project root and adding the following line to the return array.
74-
75-
```php
76-
/*
77-
|------------------------------------------------------------
78-
| Load Leaf configuration
79-
|------------------------------------------------------------
80-
|
81-
| Leaf MVC allows you to customize Leaf and it's modules using
82-
| configuration files defined in the config folder. This line
83-
| loads the configuration files and makes them available to
84-
| your application.
85-
|
86-
*/
87-
Leaf\Core::loadConsole([
88-
Leaf\Redis::commands() // [!code ++]
89-
]);
90-
```
91-
92-
Once you've done that, you should have access to a bunch of new commands from Leaf Redis. The available commands are:
93-
94-
```bash:no-line-numbers
95-
redis
96-
redis:install Create leaf redis config and .env variables
97-
redis:server Start redis server
98-
```
99-
100-
You won't need these commands in development because Leaf will automatically set up all the necessary files for you and will also start a Redis server whenever you start your application.
73+
From there, restart your server and Leaf will automatically detect the Redis package and start a Redis process alongside your application.
10174

10275
## Connecting to Redis
10376

src/docs/frontend/bareui.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ composer require leafs/bareui
4141

4242
Once installed, BareUI will be available in your Leaf app on the `template()` method. This makes it easy to use BareUI from anywhere in your app.
4343

44-
```php:no-line-numbers
45-
app()->template()->render('welcome');
46-
```
47-
4844
## Configuring BareUI
4945

5046
BareUI doesn't require any real configuration to work, but you need to tell it where to look for your templates. You can do this using the `config()` method. If you are using Leaf MVC, this has already been done for you in the `config/view.php` file, so you can skip this step.
@@ -80,10 +76,10 @@ BareUI templates are regular PHP files, so you can create your templates using P
8076

8177
## Rendering Templates
8278

83-
Once you have your template, you can render it using the `render` method. This method takes in the name of the template to render and an array of data to pass to the template.
79+
Once you have your template, you can render it using the `render()` method on Leaf's response. This method takes in the name of the template to render and an array of data to pass to the template.
8480

8581
```php:no-line-numbers
86-
echo app()->template()->render('welcome');
82+
response()->render('welcome');
8783
```
8884

8985
When rendering a template, you don't need to include the `.view.php` extension in the template name. BareUI automatically adds it for you when it looks for the template file. So, you only need to pass the name of the template without the extension, and BareUI will handle the rest!
@@ -118,15 +114,15 @@ This is an empty HTML page with a PHP tag that echoes a variable. On its own, th
118114
To pass data to this `$name` variable, you can pass an array of data as the second argument to the `render()` method. This array should contain the same keys as the variables you want to use in the template.
119115

120116
```php
121-
echo app()->template()->render('welcome', [
117+
response()->render('welcome', [
122118
'name' => 'Something',
123119
]);
124120
```
125121

126122
This will render the template and replace the `$name` variable with the value `'Something'`. You can pass as many variables as you want to the template, and they will all be available in the template file. These values can be anything from strings to arrays, objects, or even functions.
127123

128124
```php [app.php]
129-
echo app()->template()->render('products', [
125+
response()->render('products', [
130126
'items' => [
131127
['name' => 'Item 1'],
132128
['name' => 'Item 2'],

src/docs/frontend/blade.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Blade views are a pretty sweet mixture of HTML, PHP, and clean syntax. You can c
9090

9191
This should look pretty familiar if you know HTML (of course you do). The only difference is the <span v-pre>`{{ $name }}`</span> part. This is Blade's way of creating a variable in your view. When you render this view, Blade will allow you pass in a variable called `$name` and it will be displayed in place of <span v-pre>`{{ $name }}`</span>. Let's see how you can render this view.
9292

93-
<section id="leaf-zero" class="rounded-2xl shadow-md outline outline-gray-100 p-4 md:p-10">
93+
<section id="leaf-zero" class="rounded-2xl shadow-md outline outline-gray-100 dark:outline-gray-800 p-4 md:p-10 bg-[var(--vp-c-bg-alt)]">
9494
<p
9595
class="mt-4 text-3xl sm:text-4xl text-slate-900 font-extrabold tracking-tight dark:text-slate-50"
9696
>
@@ -137,37 +137,37 @@ This should look pretty familiar if you know HTML (of course you do). The only d
137137
alt=""
138138
loading="lazy"
139139
decoding="async"
140-
src="https://zero.leafphp.dev/assets/img/hero.png"
141-
class="absolute shadow-xl rounded-lg"
140+
src="https://github.com/user-attachments/assets/97489e45-dde4-4645-b074-2dfabff5d518"
141+
class="absolute shadow-xl rounded-lg border border-gray-400/20 border-t-0"
142142
style="
143-
top: 3.55556%;
144-
left: 2.22589%;
145-
width: 19.4559%;
143+
top: 0%;
144+
left: 20%;
145+
width: 46.7436%;
146146
opacity: 1;
147147
transform: none;
148148
"
149149
/><img
150150
alt=""
151151
loading="lazy"
152152
decoding="async"
153-
src="https://zero.leafphp.dev/assets/img/hero.png"
154-
class="absolute shadow-xl rounded-lg"
153+
src="https://github.com/user-attachments/assets/b1397a2e-d1ab-4b35-b2a3-d055cc8918d0"
154+
class="absolute shadow-xl rounded-lg border border-gray-400/20 !border-t-0"
155155
style="
156156
top: 0%;
157-
left: 23.6603%;
158-
width: 46.7436%;
157+
left: 57.22589%;
158+
width: 19.4559%;
159159
opacity: 1;
160160
transform: none;
161161
"
162162
/><img
163163
alt=""
164164
loading="lazy"
165165
decoding="async"
166-
src="https://zero.leafphp.dev/assets/img/hero.png"
167-
class="absolute shadow-xl rounded-lg"
166+
src="https://github.com/user-attachments/assets/49e18a39-45fd-419d-8033-050528a4052e"
167+
class="absolute shadow-xl rounded-lg border border-gray-400/20"
168168
style="
169-
top: 6.96296%;
170-
left: 72.3825%;
169+
top: 22.96296%;
170+
left: 52.3825%;
171171
width: 25.3916%;
172172
opacity: 1;
173173
transform: none;
@@ -176,11 +176,11 @@ This should look pretty familiar if you know HTML (of course you do). The only d
176176
alt=""
177177
loading="lazy"
178178
decoding="async"
179-
src="https://zero.leafphp.dev/assets/img/hero.png"
180-
class="absolute shadow-xl rounded-lg"
179+
src="https://github.com/user-attachments/assets/791fbd51-46b9-4227-8a4c-d1959b1ee984"
180+
class="absolute shadow-xl rounded-lg border border-gray-400/20"
181181
style="
182182
top: 42.8148%;
183-
left: 0%;
183+
left: 3%;
184184
width: 38.9118%;
185185
opacity: 1;
186186
transform: none;
@@ -189,28 +189,15 @@ This should look pretty familiar if you know HTML (of course you do). The only d
189189
alt=""
190190
loading="lazy"
191191
decoding="async"
192-
src="https://zero.leafphp.dev/assets/img/hero.png"
193-
class="absolute shadow-xl rounded-lg"
192+
src="https://github.com/user-attachments/assets/1c893655-f66d-49e0-b5ed-2842bcf69b43"
193+
class="absolute shadow-xl rounded-lg border border-gray-400/20"
194194
style="
195195
top: 42.8148%;
196196
left: 40.8904%;
197197
width: 36.3561%;
198198
opacity: 1;
199199
transform: none;
200200
"
201-
/><img
202-
alt=""
203-
loading="lazy"
204-
decoding="async"
205-
src="https://zero.leafphp.dev/assets/img/hero.png"
206-
class="absolute shadow-xl rounded-lg"
207-
style="
208-
top: 42.8148%;
209-
left: 79.2251%;
210-
width: 20.7749%;
211-
opacity: 1;
212-
transform: none;
213-
"
214201
/>
215202
</div>
216203
</div>
@@ -224,7 +211,7 @@ This should look pretty familiar if you know HTML (of course you do). The only d
224211
Remember we set up Blade earlier? Now we can use it to render our Blade views. Here's how you can render the `hello.blade.php` view we created earlier:
225212

226213
```php:no-line-numbers
227-
echo app()->blade()->render('hello', ['name' => 'Michael']);
214+
response()->render('hello', ['name' => 'Michael']);
228215
```
229216

230217
This will render the `hello.blade.php` view and pass in a variable called `name` with the value `Michael`. When you open the view in your browser, you should see a big "Hello, Michael" on your screen.
@@ -387,7 +374,7 @@ Likewise, the @selected directive may be used to indicate if a given select opti
387374

388375
Additionally, the @disabled directive may be used to indicate if a given element should be "disabled":
389376

390-
```blade
377+
```blade:no-line-numbers
391378
<button type="submit" @disabled($shouldBeDisabled)>Submit</button>
392379
```
393380

@@ -510,17 +497,49 @@ Blade allows you to define custom directives using the `directive()` method. Whe
510497

511498
```php
512499
app()->blade()->directive('datetime', function ($expression) {
513-
return "<?php echo tick({$expression})->format('F d, Y g:i a'); ?>";
500+
return "<?php echo tick({$expression})->format('DD MM YYYY'); ?>";
514501
});
515-
````
502+
```
503+
504+
::: details Extending blade in Leaf MVC
505+
If you use Leaf MVC, you will need to publish your view config to add custom directives. You can do this by running the following command:
506+
507+
```bash:no-line-numbers
508+
php leaf config:publish view
509+
```
510+
511+
After that, you can add your custom directives to the `config/view.php` file. Here's an example of how you can add a custom directive:
512+
513+
```php:no-line-numbers [config/view.php]
514+
...
515+
516+
/*
517+
|--------------------------------------------------------------------------
518+
| Extend view engine
519+
|--------------------------------------------------------------------------
520+
|
521+
| Some view engines like blade allow you extend the engine to
522+
| add extra functions or directives. This is just the place to
523+
| do all of that. Extend is a function that accepts an instance
524+
| of your view engine which you can 'extend'
525+
|
526+
*/
527+
'extend' => function (\Leaf\Blade $engine) {
528+
$engine->directive('datetime', function ($expression) {
529+
return "<?php echo tick({$expression})->format('DD MM YYYY'); ?>";
530+
});
531+
},
532+
```
533+
534+
:::
516535

517536
Which allows you to use the following in your blade template:
518537

519538
```blade:no-line-numbers
520539
Current date: @datetime($date)
521540
```
522541

523-
This will output the current date in the format `F d, Y g:i a`. You can define as many custom directives as you want to make your Blade views more powerful.
542+
This will output the current date in the format `DD MM YYYY`. You can define as many custom directives as you want to make your Blade views more powerful.
524543

525544
## Conclusion
526545

0 commit comments

Comments
 (0)