|
1 | 1 | # Custom Libraries |
| 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. |
| 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. |
| 6 | + |
| 7 | +## Autoloading Libraries |
| 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: |
| 10 | + |
| 11 | +```php |
| 12 | +// \Leaf\Core::loadLibs(); |
| 13 | +``` |
| 14 | + |
| 15 | +Once you do so, you can start creating your own libraries. |
| 16 | + |
| 17 | +## Creating a Library |
| 18 | + |
| 19 | +To create a library, simply create a new file in the `lib` folder. For example, let's create a library called `Math.php`: |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +namespace MyRandom\Name\Space; |
| 25 | + |
| 26 | +class Math { |
| 27 | + public static function add($a, $b) { |
| 28 | + return $a + $b; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Using a Library |
| 34 | + |
| 35 | +To use a library, you must first import it. You can then use it like any other class. For example, let's import the `Math` library we created above: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | + |
| 40 | +namespace App\Controllers; |
| 41 | + |
| 42 | +use MyRandom\Name\Space\Math; |
| 43 | + |
| 44 | +class HomeController extends Controller { |
| 45 | + public function index() { |
| 46 | + $sum = Math::add(1, 2); |
| 47 | + echo view('home', ['sum' => $sum]); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Library Structure |
| 53 | + |
| 54 | +As mentioned above, libraries can be just about anything. They are completely based on your own preference. However, it is recommended that you keep your libraries as simple as possible. Below is the same `Math` library from above, but this time it is a simple function instead of a class: |
| 55 | + |
| 56 | +```php |
| 57 | +<?php |
| 58 | + |
| 59 | +namespace Lib; |
| 60 | + |
| 61 | +function add($a, $b) { |
| 62 | + return $a + $b; |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +You can then use this library like so: |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | + |
| 71 | +namespace App\Controllers; |
| 72 | + |
| 73 | +use function Lib\add; |
| 74 | + |
| 75 | +class HomeController extends Controller { |
| 76 | + public function index() { |
| 77 | + $sum = add(1, 2); |
| 78 | + echo view('home', ['sum' => $sum]); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
0 commit comments