phplaravel-5userfrosting

Using Laravel Facades with UserFrosting


Have recently starting using UserFrosting to as part of a project and I'm having some problems using Facades within UserFrosting and would appreciate some help if possible.

I am attempting to use the File facade from within a UserFrosting controller to create a folder on the local filesystem using the following code

use Illuminate\Support\Facades\File; ...... $directoryCreated = File::makeDirectory($directoryPath);

However at runtime i get the following error

PHP Fatal error: Call to a member function makeDirectory() on null in /var/www/test-app/userfrosting/vendor/illuminate/support/Facades/Facade.php on line 210

It seems that the UserFrosting app does not recognise the File facade (or any other facacde - i also tried Storage) and it has not been registered with the app.

Is it possible to use the facade classes with UserFrosting?

If so do I have to register them somewhere within the UserFrosting app config?

Any direction pointers would be greatly appreciated.

Thanks in advance!


Solution

  • From the Facade documentation:

    Laravel "facades" serve as "static proxies" to underlying classes in the service container...

    So, it looks like Laravel's facades depend on Laravel's service container. You can read more about how Laravel sets up the default facades here: https://www.sitepoint.com/how-laravel-facades-work-and-how-to-use-them-elsewhere/#how-laravel-aliases-the-facades

    Unfortunately, UserFrosting does not use Laravel's service container. Rather, it uses Slim, which has its own service container. In Slim v2 (which UF 0.3.1 uses), the Slim application itself is the service container.

    You can define services for UF in initialize.php. So, you might try something like:

    $app->filesystem = new \Illuminate\Filesystem\Filesystem();

    Then later, you can use the filesystem service like:

    $app->filesystem->makeDirectory($directoryPath);