I'm new to Lumen and Laravel, but I have to write a REST API using Lumen. I've set up a controller and I'm having a problem using the logger. I've followed the documentation: Lumen docs
This is my controller app/Http/Controllers/DocumentsController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Log;
class DocumentsController extends Controller
{
public function index()
{
Log::info('test');
return response()->json(['result' => 'Oh hey!']);
}
}
If I run this I'm getting an error saying:
FatalErrorException in DocumentsController.php line 22: Class 'Log' not found
So there seems to be something wrong with the Log facade (not quite sure how those work yet in Laravel/Lumen).
But if I change the Log::info() call, to manually pull the log service out of the DI container then it works:
$app = app();
$app->make('log')->info('test');
Any ideas as to why the facade method as described in the official documentation isn't working?
Doh and of course 5 minutes after posting this question I figured it out. I noticed in the Application base class (vendor/laravel/lumen-framework/src/Application.php) the following aliases:
public function withFacades()
{
Facade::setFacadeApplication($this);
if (! static::$aliasesRegistered) {
static::$aliasesRegistered = true;
class_alias('Illuminate\Support\Facades\Auth', 'Auth');
class_alias('Illuminate\Support\Facades\Cache', 'Cache');
class_alias('Illuminate\Support\Facades\DB', 'DB');
class_alias('Illuminate\Support\Facades\Event', 'Event');
class_alias('Illuminate\Support\Facades\Gate', 'Gate');
class_alias('Illuminate\Support\Facades\Log', 'Log');
class_alias('Illuminate\Support\Facades\Queue', 'Queue');
class_alias('Illuminate\Support\Facades\Schema', 'Schema');
class_alias('Illuminate\Support\Facades\URL', 'URL');
class_alias('Illuminate\Support\Facades\Validator', 'Validator');
}
}
This method was never called however, because in bootstrap/app.php the $this->withFacades() call is commented out by default.
I uncommented it and now it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With