Skip to content

Commit 6265feb

Browse files
authored
Merge pull request #100 from leafsphp/staging
Staging
2 parents afcd33b + 7e50db4 commit 6265feb

File tree

6 files changed

+151
-26
lines changed

6 files changed

+151
-26
lines changed

src/docs/database/builder.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Although you can write raw queries using the `query()` method, there's no fun in
66

77
This is something you would usually want to do outside of your application, but there are a few rare cases where you might want to create a database from within your application. Leaf DB provides a `create()` method that allows you to do just that.
88

9-
```php
9+
```php:no-line-numbers
1010
db()->create('dbname')->execute();
1111
```
1212

@@ -26,7 +26,7 @@ db()
2626

2727
Dropping a database is the opposite of creating one. It deletes the database and all its contents. Leaf DB provides a `drop()` method that allows you to do this.
2828

29-
```php
29+
```php:no-line-numbers
3030
db()->drop('dbname')->execute();
3131
```
3232

@@ -40,13 +40,13 @@ This method needs the name of the table you want to add data to, like "users", a
4040

4141
Here's an example:
4242

43-
```php
43+
```php:no-line-numbers
4444
db()->insert('users')->params(['username' => 'mychi']);
4545
```
4646

4747
This is equivalent to the following SQL query:
4848

49-
```sql
49+
```sql:no-line-numbers
5050
INSERT INTO users (username) VALUES ('mychi')
5151
```
5252

@@ -89,17 +89,19 @@ db()->insert('users')->params(['username' => 'mychi'])->execute();
8989
$lastId = db()->lastInsertId();
9090
```
9191

92+
Note that this may not work correctly if your database uses non-auto-incrementing IDs like UUIDs or ULIDs.
93+
9294
## Reading data from a database
9395

9496
Reading from a database means retrieving data stored in a table. Leaf DB provides a `select()` method that allows you to build a query to retrieve data from a table. The `select()` method takes the name of the table you want to read from as its argument.
9597

96-
```php
98+
```php:no-line-numbers
9799
db()->select('users')->all();
98100
```
99101

100102
This will return all the rows in the users table. You can also specify the columns you want to return by passing them as the second argument to the `select()` method.
101103

102-
```php
104+
```php:no-line-numbers
103105
db()->select('users', 'name, created_at')->all();
104106
```
105107

@@ -172,7 +174,7 @@ db()
172174

173175
Almost every database table has an `id` column that uniquely identifies each row. Leaf DB provides a `find()` method that allows you to retrieve a row by its `id`.
174176

175-
```php
177+
```php:no-line-numbers
176178
db()->select('users')->find(1);
177179
```
178180

@@ -209,7 +211,7 @@ Deleting data from a database works by finding the data you want to delete and t
209211

210212
Here's an example:
211213

212-
```php
214+
```php:no-line-numbers
213215
db()->delete('users')->execute(); // careful now 🙂
214216
```
215217

@@ -259,6 +261,10 @@ if ($success) {
259261

260262
This is useful especially when you have a set of queries that rely on third party influence.
261263

264+
::: warning Rollback not working
265+
Transactions will only work correctly if your queries use Leaf DB. This is because your queries need to use the same database connection to be able to be rolled back. This means you can't use transactions with your Leaf MVC models at the moment, but this may change in the future.
266+
:::
267+
262268
## Hiding columns from results
263269

264270
Sometimes you might want to hide certain columns from the results of a query. For instance, you might want to hide the password column from the results of a query on the users table. Leaf DB provides a `hide()` method that allows you to do this.
@@ -342,6 +348,25 @@ db()
342348
->all();
343349
```
344350

351+
## Counting results
352+
353+
You can count the number of results returned by a query using the `count()` method.
354+
355+
```php
356+
db()
357+
->select('users')
358+
->count();
359+
```
360+
361+
Or even with complex queries:
362+
363+
```php
364+
db()
365+
->select('users')
366+
->where('age', '>', 20)
367+
->count();
368+
```
369+
345370
## Error Handling
346371

347372
There are lots of times where your query might fail. This could be because of a syntax error, a missing table, or a missing column. Leaf DB provides an `errors()` method that allows you to get the error message if your query fails.

src/docs/database/redis.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ composer require leafs/redis
4343

4444
Once that's done, we can start using Leaf Redis in our Leaf application. Just like any other database, we need to initialize a connection to Redis before we can start using it.
4545

46-
```php
46+
```php:no-line-numbers
4747
redis()->connect();
4848
```
4949

@@ -53,7 +53,26 @@ This will initialize a new Redis connection. From there, you can start storing a
5353

5454
If you're using Leaf MVC, you can add on some extra features to your setup. Leaf Redis comes with a few commands that you can attach to your Aloe CLI. You can do this by heading over to the `app/console/Commands.php` file in your Leaf MVC app and adding the following line to the return array.
5555

56-
```php
56+
::: code-group
57+
58+
```php [Leaf MVC 3.8 and above]
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Load Leaf configuration
62+
|--------------------------------------------------------------------------
63+
|
64+
| Leaf MVC allows you to customize Leaf and it's modules using
65+
| configuration files defined in the config folder. This line
66+
| loads the configuration files and makes them available to
67+
| your application.
68+
|
69+
*/
70+
Leaf\Core::loadConsole([
71+
Leaf\Redis::commands() // [!code ++]
72+
]);
73+
```
74+
75+
```php [Leaf MVC 3.7 and below]
5776
<?php
5877

5978
namespace App\Console;
@@ -71,15 +90,17 @@ class Commands
7190
{
7291
$console->register([
7392
ExampleCommand::class,
74-
\Leaf\Redis::commands() // [!code ++]
93+
Leaf\Redis::commands() // [!code ++]
7594
]);
7695
}
7796
}
7897
```
7998

99+
:::
100+
80101
Once you've done that, you should have access to a bunch of new commands from Leaf Redis. The available commands are:
81102

82-
```bash
103+
```bash:no-line-numbers
83104
redis
84105
redis:install Create leaf redis config and .env variables
85106
redis:server Start redis server
@@ -212,15 +233,15 @@ redis()->connect([
212233

213234
You can check if your Redis connection is working by using the `ping()` method. The `ping()` method returns a string with the message "PONG" if the connection is successful.
214235

215-
```php
236+
```php:no-line-numbers
216237
echo redis()->ping();
217238
```
218239

219240
## Setting values
220241

221242
You can set values in Redis using the `set()` method. The `set()` method takes in a key and a value.
222243

223-
```php
244+
```php:no-line-numbers
224245
redis()->set('name', 'Michael');
225246
```
226247

src/docs/frontend/blade.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Blade is a templating engine included with Laravel that helps you create dynamic
1010

1111
Leaf Blade is a port of the [jenssegers/blade](https://github.com/jenssegers/blade) package that allows you to use blade templates in your Leaf PHP projects.
1212

13+
<!-- Leaf Blade is an adaptation of the original Blade package, which provides a powerful engine that is familiar to most PHP developers. While similar, Leaf Blade has some differences from the original Blade package, so be sure to keep this documentation handy. -->
14+
1315
::: details New to Blade?
1416

1517
This video by The Net Ninja will help you get started with blade.
@@ -27,7 +29,7 @@ This video by The Net Ninja will help you get started with blade.
2729

2830
::: info Blade + Leaf MVC
2931

30-
Blade comes with Leaf MVC out of the box, fully configured and ready to use. However, if you're using Leaf Core, you'll need to set up Blade yourself.
32+
Blade comes with Leaf MVC out of the box, fully configured and ready to use, so you can skip this section if you're using Leaf MVC.
3133

3234
:::
3335

@@ -85,7 +87,7 @@ Blade views are a pretty sweet mixture of HTML, PHP, and clean syntax. You can c
8587

8688
:::
8789

88-
This should look pretty familiar if you know HTML (of course you do). The only difference is the `{{ $name }}` 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 `{{ $name }}`. Let's see how you can render this view.
90+
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.
8991

9092
## Rendering Blade Views
9193

@@ -97,6 +99,47 @@ echo app()->blade()->render('hello', ['name' => 'Michael']);
9799

98100
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.
99101

102+
<!-- ## Directives included in Leaf Blade
103+
104+
As mentioned earlier, Leaf Blade is an adaptation of the original Blade package, so it includes some directives that are not available in the original Blade package. Here are some of the directives included in Leaf Blade:
105+
106+
### `@csrf`
107+
108+
The `@csrf` directive generates a hidden CSRF token field for forms. This is useful when you want to include a CSRF token in your form.
109+
110+
```blade:no-line-numbers
111+
<form method="POST">
112+
@csrf
113+
...
114+
</form>
115+
```
116+
117+
### `@method`
118+
119+
The `@method` directive generates a hidden input field with the value of the method you specify. This is useful when you want to use methods other than `GET` and `POST` in your forms.
120+
121+
```blade:no-line-numbers
122+
<form method="POST">
123+
@method('PUT')
124+
...
125+
</form>
126+
127+
<form method="POST">
128+
@method('DELETE')
129+
...
130+
</form>
131+
```
132+
133+
### `@submit`
134+
135+
The `@submit` directive allows you to wrap an item with a form that submits when the item is clicked. This is useful when you want to redirect to a post route when an item is clicked.
136+
137+
```blade:no-line-numbers
138+
@submit('DELETE', '/posts/1')
139+
<button>Delete</button>
140+
@endsubmit
141+
``` -->
142+
100143
## Extending Blade Views
101144

102145
Blade allows you to define custom directives using the `directive()` method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains. The callback is free to return the value of its contents however you like:

src/docs/http/response.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ Also, Leaf's response object provides a clean and consistent API for creating re
5454

5555
## Plain text responses
5656

57-
Plain text responses can be created using the `text()` method. This method accepts 2 parameters:
57+
Plain text responses can be created using the `plain()` method. This method accepts 2 parameters:
5858

5959
- a string as text to output
6060
- an optional status code (defaults to 200/OK)
6161

6262
::: code-group
6363

64-
```php [Functional Mode]
65-
response()->text('Hello, world!');
64+
```php:no-line-numbers [Functional Mode]
65+
response()->plain('Hello, world!');
6666
```
6767

68-
```php [Leaf Instance]
69-
$app->response()->text('Hello, world!');
68+
```php:no-line-numbers [Leaf Instance]
69+
$app->response()->plain('Hello, world!');
7070
```
7171

7272
:::
@@ -258,6 +258,38 @@ $app->response()->xml('<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
258258

259259
:::
260260

261+
### Custom responses
262+
263+
If you need to create a custom response, which is not covered by the methods above, you can use the `echo()` method. This method accepts 2 parameters:
264+
265+
- the content to output
266+
- an optional status code (defaults to 200/OK)
267+
268+
::: code-group
269+
270+
```php:no-line-numbers [Functional Mode]
271+
response()
272+
->withHeader([
273+
'Content-Type' => 'application/pdf',
274+
'Content-Length' => $dataLength,
275+
'Content-Disposition' => "inline; filename=\"$filename\""
276+
])
277+
->echo($rawData);
278+
```
279+
280+
```php:no-line-numbers [Leaf Instance]
281+
$app
282+
->response()
283+
->withHeader([
284+
'Content-Type' => 'application/pdf',
285+
'Content-Length' => $dataLength,
286+
'Content-Disposition' => "inline; filename=\"$filename\""
287+
])
288+
->echo($rawData);
289+
```
290+
291+
:::
292+
261293
## Headers
262294

263295
Headers are a way for your server to send additional information along with your request. This information can be anything from the type of content you're sending back, to the status code of your response, to the type of server you're using.

src/docs/mvc/commands.md

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

85-
You can register this component by heading over to the `app/console/commands.php` file and adding the command to the `register()` method.
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
8686

8787
```php
8888
<?php

src/docs/mvc/console.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ These commands handle your frontend setup, building, and serving.
238238
This is a list of every command available in Aloe. To view this list from your terminal, run `php leaf list`.
239239

240240
```bash:no-line-numbers
241-
Leaf MVC v3.5.0
241+
Leaf MVC v3.8.0
242242
243243
Usage:
244244
command [options] [arguments]
@@ -253,16 +253,19 @@ Options:
253253
254254
Available commands:
255255
completion Dump the shell completion script
256-
example example command's description
257256
help Display help for a command
258257
interact Interact with your application
258+
link Create a symbolic link for the storage directory
259259
list List commands
260260
serve Start the leaf development server
261261
app
262262
app:down Place app in maintainance mode
263263
app:up Remove app from maintainance mode
264264
auth
265265
auth:scaffold Scaffold basic app authentication
266+
config
267+
config:lib Setup Leaf MVC to use external libraries
268+
config:mail Install leaf mail and setup mail config
266269
d
267270
d:command Delete a console command
268271
d:controller Delete a controller
@@ -286,12 +289,13 @@ Available commands:
286289
g:factory Create a new model factory
287290
g:helper Create a new helper class
288291
g:mailer Create a new mailer
292+
g:middleware Create a new application middleware
289293
g:migration Create a new migration file
290294
g:model Create a new model class
291295
g:seed Create a new seed file
292296
g:template Create a new view file
293-
mail
294-
mail:setup Install leaf mail and setup mail config
297+
key
298+
key:generate Run your frontend dev server
295299
view
296300
view:build Run your frontend dev server
297301
view:dev [view:serve] Run your frontend dev server

0 commit comments

Comments
 (0)