|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Facades |
| 6 | + |
| 7 | +Lumberjack provides a custom implementation of Facades, based on the now-deprecated [Blast Facades](https://github.com/phpthinktank/blast-facades) library. |
| 8 | + |
| 9 | +## Creating a Facade |
| 10 | + |
| 11 | +Facades provide a simple static API to an object that has been registered into the container. For example to setup a facade you would first use a Service Provider to register an instance of your class into the container: |
| 12 | + |
| 13 | +```php |
| 14 | +namespace Rareloop\Lumberjack\Providers; |
| 15 | + |
| 16 | +use Monolog\Logger; |
| 17 | +use Rareloop\Lumberjack\Application; |
| 18 | + |
| 19 | +class LogServiceProvider extends ServiceProvider |
| 20 | +{ |
| 21 | + public function register() |
| 22 | + { |
| 23 | + // Create object instance and bind into container |
| 24 | + $this->app->bind('logger', new Logger('app')); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Then create a Facade subclass and tell it which key to use to retrieve your class instance: |
| 30 | + |
| 31 | +```php |
| 32 | +namespace Rareloop\Lumberjack\Facades; |
| 33 | + |
| 34 | +use Rareloop\Lumberjack\Facades\AbstractFacade; |
| 35 | + |
| 36 | +class Log extends AbstractFacade |
| 37 | +{ |
| 38 | + protected static function accessor() |
| 39 | + { |
| 40 | + return 'logger'; |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Available Facades |
| 46 | + |
| 47 | +Lumberjack comes with a handful of useful Facades. Below you can see which class the Facade references and what it is bound to the container under. |
| 48 | + |
| 49 | +| Facade | Class Reference | Container binding | |
| 50 | +| :------ | :------------------------------------------- | :---------------- | |
| 51 | +| Config | `Rareloop\Lumberjack\Config` | `config` | |
| 52 | +| Log | `Monolog\Logger` | `logger` | |
| 53 | +| Router | `Rareloop\Lumberjack\Http\Route` | `router` | |
| 54 | +| Session | `Rareloop\Lumberjack\Session\SessionManager` | `session` | |
| 55 | + |
| 56 | +### Example usage |
| 57 | + |
| 58 | +All of Lumberjack's Facades live under the `Rareloop\Lumberjack\Facades` namespace. |
| 59 | + |
| 60 | +#### Config |
| 61 | + |
| 62 | +The `Config` facade allows you to get and set values from your config. |
| 63 | + |
| 64 | +```php |
| 65 | +use Rareloop\Lumberjack\Facades\Config; |
| 66 | + |
| 67 | +Config::set('app.debug', false); |
| 68 | + |
| 69 | +$value = Config::get('app.debug'); |
| 70 | +``` |
| 71 | + |
| 72 | +Config is also available as a [Helper](/the-basics/helpers#config) |
| 73 | + |
| 74 | +#### Log |
| 75 | + |
| 76 | +The `Log` facade allows you to use the [PSR-3](https://www.php-fig.org/psr/psr-3/) Logger [Monolog](https://github.com/Seldaek/monolog). |
| 77 | + |
| 78 | +```php |
| 79 | +use Rareloop\Lumberjack\Facades\Log; |
| 80 | + |
| 81 | +$value = Log::warning('Oops! Something went wrong'); |
| 82 | +``` |
| 83 | + |
| 84 | +#### Router |
| 85 | + |
| 86 | +The `Router` facade allows you to create custom routes or get the URLs for your routes. |
| 87 | + |
| 88 | +```php |
| 89 | +use Rareloop\Lumberjack\Facades\Router; |
| 90 | + |
| 91 | +Router::get('posts/all', function () {})->name('posts.index'); |
| 92 | + |
| 93 | +$url = Router::url('posts.index'); |
| 94 | +``` |
| 95 | + |
| 96 | +See [Creating Routes](/the-basics/routing#creating-routes) for more information. |
| 97 | + |
| 98 | +#### Session |
| 99 | + |
| 100 | +The `Session` facade allows you to retrieve and store data in the current session. |
| 101 | + |
| 102 | +```php |
| 103 | +use Rareloop\Lumberjack\Facades\Session; |
| 104 | + |
| 105 | +Session::put('name', 'Chris'); |
| 106 | + |
| 107 | +$name = Session::get('name'); |
| 108 | +``` |
| 109 | + |
| 110 | +Session is also available as a [Helper](/the-basics/helpers#session) |
0 commit comments