Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fbrip's avatar
Level 1

laravel 5.3 how to render html content of a page in a view with a function

I am facing a rendering problem... in my product.blade.php I would like to show in a part of the page the html content from an external source without iframe. I want to make a call like :

{!! View::make('pages.viewer', ['docUrl' => url('get-DocHtml/' . $primaryAttachment->id)]) !!}

In my route : Route::get('get-DocHtml/{id}', 'PagesController@getDocHtml');

In PagesController.php : public function getDocHtml($id){ $attachment = Attachment::find($id); $filepath = Storage::disk('S3')->url($attachment->filename.'/test.html'); return \Response::make(file_get_contents($filePath), 200, [ 'Content-Type' => 'text/html; charset=utf-8' ]); }

I don't know how to make it render in viewer.blade.php or directly in my product.blade.php like a all to the function and render it directly...

Is someone has any idea how to render it (without helpers) ?

Thanks for your help.

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

break it into steps then you can check each.

In the controller, get the remote content and assign it to a variable. Use Curl, Guzzle or just fileGetContents. Stick with a fixed ID to start with

just return or dd() the variable and check its fetching something.

Get the ID from the request and check the controller correctly fetches variable content depending on the ID requested

Create the view and inject the content using {!! blade tags if you trust the content (dangerous) as this will render exactly what you grabbed. Bear in mind that links and img references in the content are likely to be broken

In the controller return View with the variable you assigned the content to

Consider caching the data fetched for a short time.

fbrip's avatar
Level 1

Thanks for your help. I just tried to dd() the variable, it doesn't work... I may not do the right thing regarding this. Shall I delete this in my product page, to make it simple : {!! View::make('pages.viewer', ['docUrl' => url('get-DocHtml/' . $primaryAttachment->id)]) !!} And delete in my route : Route::get('get-DocHtml/{id}', 'PagesController@getDocHtml');

Can I just have a call like {!! $external !!} (I have an error "Method Illuminate\View\View::__toString() must not throw an exception")

and use a function with variable like this in my PagesController.php :

public function getDocHtml($id) { $attachment = Attachment::find($id); $filepath = Storage::disk('S3')->url($attachment->filename.'/test.html'); $file = file_get_contents($filePath) return Viewe::make('pages.viewer', ['external' => $file]);

How can I have in my product page this html content ?

Snapey's avatar

Actually, forget what I said before.

First learn how to use PHP and Laravel with some exercises and learn how to build a simple MVC application.

fbrip's avatar
Level 1

Thanks for your first answer. Everything is working. I actually change my controller as my variable was empty with the right function $filepath = Storage::disk('S3')->url($attachment->filename.'/test.html'); $file = file_get_contents($filePath) return view('pages.product')->withExternal($file);

and in my product blade, I just have a {!! $external !!}

Please or to participate in this conversation.