I wonder why I can't access $app
from my controllers.
I'm using this service provider that I register in app.php
.
$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
The documentations tells me to use it as follows (from a controller):
$pdf = $app->make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
or:
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
Either way gives me the following error
Undefined variable: app
How can I make this work?
In Lumen you should use the app()
helper method (see Lumen docs) to get an instance of a class.
Here is an example, that I used.
routes/web.php
<?php
/** @var \Laravel\Lumen\Routing\Router $router */
$router->get('/', 'ExampleController@index');
app/Http/Controllers/ExampleController.php
<?php
namespace App\Http\Controllers;
use Dompdf\Dompdf;
class ExampleController extends Controller
{
public function index()
{
/** @var Dompdf $pdf */
$pdf = app('dompdf');
$pdf->loadHTML('<h1>Test</h1>');
// Output whatever you like
return $pdf->outputHtml();
}
}