|
1 | 1 | # Custom Libraries |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 6 |
|
7 | 7 | ## Autoloading Libraries |
8 | 8 |
|
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: |
10 | 19 |
|
11 | 20 | ```php |
12 | | -// \Leaf\Core::loadLibs(); |
| 21 | +// \Leaf\Core::loadLibs(); |
13 | 22 | ``` |
14 | 23 |
|
15 | | -Once you do so, you can start creating your own libraries. |
| 24 | +::: |
16 | 25 |
|
17 | 26 | ## Creating a Library |
18 | 27 |
|
@@ -79,3 +88,36 @@ class HomeController extends Controller { |
79 | 88 | } |
80 | 89 | } |
81 | 90 | ``` |
| 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. |
0 commit comments