Skip to content

Commit

Permalink
Merge pull request #100 from leafsphp/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
mychidarko authored Dec 6, 2024
2 parents afcd33b + 7e50db4 commit 6265feb
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 26 deletions.
41 changes: 33 additions & 8 deletions src/docs/database/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Although you can write raw queries using the `query()` method, there's no fun in

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.

```php
```php:no-line-numbers
db()->create('dbname')->execute();
```

Expand All @@ -26,7 +26,7 @@ db()

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.

```php
```php:no-line-numbers
db()->drop('dbname')->execute();
```

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

Here's an example:

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

This is equivalent to the following SQL query:

```sql
```sql:no-line-numbers
INSERT INTO users (username) VALUES ('mychi')
```

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

Note that this may not work correctly if your database uses non-auto-incrementing IDs like UUIDs or ULIDs.

## Reading data from a database

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.

```php
```php:no-line-numbers
db()->select('users')->all();
```

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.

```php
```php:no-line-numbers
db()->select('users', 'name, created_at')->all();
```

Expand Down Expand Up @@ -172,7 +174,7 @@ db()

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`.

```php
```php:no-line-numbers
db()->select('users')->find(1);
```

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

Here's an example:

```php
```php:no-line-numbers
db()->delete('users')->execute(); // careful now 🙂
```

Expand Down Expand Up @@ -259,6 +261,10 @@ if ($success) {

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

::: warning Rollback not working
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.
:::

## Hiding columns from results

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.
Expand Down Expand Up @@ -342,6 +348,25 @@ db()
->all();
```

## Counting results

You can count the number of results returned by a query using the `count()` method.

```php
db()
->select('users')
->count();
```

Or even with complex queries:

```php
db()
->select('users')
->where('age', '>', 20)
->count();
```

## Error Handling

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.
Expand Down
33 changes: 27 additions & 6 deletions src/docs/database/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ composer require leafs/redis

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.

```php
```php:no-line-numbers
redis()->connect();
```

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

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.

```php
::: code-group

```php [Leaf MVC 3.8 and above]
/*
|--------------------------------------------------------------------------
| Load Leaf configuration
|--------------------------------------------------------------------------
|
| Leaf MVC allows you to customize Leaf and it's modules using
| configuration files defined in the config folder. This line
| loads the configuration files and makes them available to
| your application.
|
*/
Leaf\Core::loadConsole([
Leaf\Redis::commands() // [!code ++]
]);
```

```php [Leaf MVC 3.7 and below]
<?php

namespace App\Console;
Expand All @@ -71,15 +90,17 @@ class Commands
{
$console->register([
ExampleCommand::class,
\Leaf\Redis::commands() // [!code ++]
Leaf\Redis::commands() // [!code ++]
]);
}
}
```

:::

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

```bash
```bash:no-line-numbers
redis
redis:install Create leaf redis config and .env variables
redis:server Start redis server
Expand Down Expand Up @@ -212,15 +233,15 @@ redis()->connect([

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.

```php
```php:no-line-numbers
echo redis()->ping();
```

## Setting values

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

```php
```php:no-line-numbers
redis()->set('name', 'Michael');
```

Expand Down
47 changes: 45 additions & 2 deletions src/docs/frontend/blade.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Blade is a templating engine included with Laravel that helps you create dynamic

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.

<!-- 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. -->

::: details New to Blade?

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

::: info Blade + Leaf MVC

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

:::

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

:::

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

## Rendering Blade Views

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

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.

<!-- ## Directives included in Leaf Blade
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:
### `@csrf`
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.
```blade:no-line-numbers
<form method="POST">
@csrf
...
</form>
```
### `@method`
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.
```blade:no-line-numbers
<form method="POST">
@method('PUT')
...
</form>
<form method="POST">
@method('DELETE')
...
</form>
```
### `@submit`
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.
```blade:no-line-numbers
@submit('DELETE', '/posts/1')
<button>Delete</button>
@endsubmit
``` -->

## Extending Blade Views

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:
Expand Down
42 changes: 37 additions & 5 deletions src/docs/http/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ Also, Leaf's response object provides a clean and consistent API for creating re

## Plain text responses

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

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

::: code-group

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

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

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

:::

### Custom responses

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:

- the content to output
- an optional status code (defaults to 200/OK)

::: code-group

```php:no-line-numbers [Functional Mode]
response()
->withHeader([
'Content-Type' => 'application/pdf',
'Content-Length' => $dataLength,
'Content-Disposition' => "inline; filename=\"$filename\""
])
->echo($rawData);
```

```php:no-line-numbers [Leaf Instance]
$app
->response()
->withHeader([
'Content-Type' => 'application/pdf',
'Content-Length' => $dataLength,
'Content-Disposition' => "inline; filename=\"$filename\""
])
->echo($rawData);
```

:::

## Headers

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.
Expand Down
2 changes: 1 addition & 1 deletion src/docs/mvc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CachePurgeCommand extends Command
}
```

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

```php
<?php
Expand Down
12 changes: 8 additions & 4 deletions src/docs/mvc/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ These commands handle your frontend setup, building, and serving.
This is a list of every command available in Aloe. To view this list from your terminal, run `php leaf list`.

```bash:no-line-numbers
Leaf MVC v3.5.0
Leaf MVC v3.8.0
Usage:
command [options] [arguments]
Expand All @@ -253,16 +253,19 @@ Options:
Available commands:
completion Dump the shell completion script
example example command's description
help Display help for a command
interact Interact with your application
link Create a symbolic link for the storage directory
list List commands
serve Start the leaf development server
app
app:down Place app in maintainance mode
app:up Remove app from maintainance mode
auth
auth:scaffold Scaffold basic app authentication
config
config:lib Setup Leaf MVC to use external libraries
config:mail Install leaf mail and setup mail config
d
d:command Delete a console command
d:controller Delete a controller
Expand All @@ -286,12 +289,13 @@ Available commands:
g:factory Create a new model factory
g:helper Create a new helper class
g:mailer Create a new mailer
g:middleware Create a new application middleware
g:migration Create a new migration file
g:model Create a new model class
g:seed Create a new seed file
g:template Create a new view file
mail
mail:setup Install leaf mail and setup mail config
key
key:generate Run your frontend dev server
view
view:build Run your frontend dev server
view:dev [view:serve] Run your frontend dev server
Expand Down

0 comments on commit 6265feb

Please sign in to comment.