Skip to content

Commit 1b5c230

Browse files
committed
Merge branch 'staging'
2 parents 7401d00 + f2c3ff0 commit 1b5c230

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/docs/mvc/libraries.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
# Custom Libraries
22

3-
Sometimes you might want to write some application logic that doesn't fit into a controller, model, or helpers. It makes sense to create a custom library for this functionality. For example, you might want to create a library that calculates the distance between two points on a map. You could then use this library in any controller, helper, or view. Leaf MVC and Leaf API both come with a `lib` folder where you can store your custom libraries.
3+
We usually recommend abstracting repetitive code into helpers, but sometimes you might want to write some application logic that doesn't fit into a controller, model, or helpers. It makes sense to create a custom library for this functionality. For example, you might want to create a library that calculates the distance between two points on a map. You could then use this library in any controller, helper, or view.
44

5-
Custom libraries are not stored in the `app` folder because they are not part of the application's core functionality. They are more like helpers, however, unlike helpers they can be full classes, functions or just data structures and also require no structured namespace.
5+
Custom libraries are not stored in the `app` folder because they are not autoloaded by Leaf MVC. Instead, you can store them in the `lib` folder which Leaf will then pick up. Things are done this way because you may not always have a library that follows an autoloadable structure and may need to be `require`d manually.
66

77
## Autoloading Libraries
88

9-
Leaf MVC and Leaf API will only automatically load your libraries for you if you tell it to do so. You can do this by **uncommenting** the following line in your `public/index.php` file:
9+
Leaf MVC only loads items in the `app` folder by default. To add any external library to your project, you need to set Leaf MVC up for it using the console. You can do this by running the following command:
10+
11+
```bash:no-line-numbers
12+
php leaf config:lib
13+
```
14+
15+
That's it! A `lib` folder will be created in your application root and Leaf will now autoload any library you place in this folder.
16+
17+
::: info Older Leaf MVC versions
18+
If you are using an older version of Leaf MVC where you don't have the `config:lib` command, you simply need to head over to your `public/index.php` file and uncomment the following line:
1019

1120
```php
12-
// \Leaf\Core::loadLibs();
21+
// \Leaf\Core::loadLibs();
1322
```
1423

15-
Once you do so, you can start creating your own libraries.
24+
:::
1625

1726
## Creating a Library
1827

@@ -79,3 +88,36 @@ class HomeController extends Controller {
7988
}
8089
}
8190
```
91+
92+
## Using a non-autoloadable library
93+
94+
Some older libraries may not follow the autoloadable structure but are linked together using `require` statements. You can still use these libraries in your Leaf MVC application. To use such a library, you need to add it's index file to the `lib` folder and require any other files it needs in the index file. For example, let's say you have a library called `MyLibrary` that has the following structure:
95+
96+
```bash:no-line-numbers
97+
MyLibrary/
98+
index.php
99+
file1.php
100+
file2.php
101+
```
102+
103+
You can add this library to your Leaf MVC application by moving the `MyLibrary` folder to the `lib` folder and creating an `index.php` file in the `MyLibrary` folder that requires the other files:
104+
105+
```bash:no-line-numbers
106+
app/
107+
lib/
108+
mylibrary.php
109+
MyLibrary/
110+
index.php
111+
file1.php
112+
file2.php
113+
```
114+
115+
And in `lib/mylibrary.php`:
116+
117+
```php
118+
<?php
119+
120+
require __DIR__ . '/MyLibrary/index.php';
121+
```
122+
123+
From here, you can use `MyLibrary` and all it's functions in your Leaf MVC application.

src/public/_redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
/modules/views/* /docs/frontend/:splat
2020
/community/docs-writing-guide /community/guide
2121
/community/contribute /community/guide
22+
docs/mvc/schema.html /docs/database/schema

0 commit comments

Comments
 (0)