1

I have php file template that I need to fill with some data and export as "rendered_view.php". It need to be done automatically every day. So I'm trying to use the Laravel Scheduler.

So I have:

View "view_to_render.blade.php"

<?
    $someVariable = "{{$variable}}";
    require_once("includes/php_file.php");
?>

Controller "MiscController.php"

public function testRenderView(){
    file_put_contents(public_path('rendered_view.php'), view('view_to_render', ['variable' => '123456'])->render());
}

Route

Route::get('testRenderView', 'MiscController@testRenderView');

Console/Kernel.php

$schedule->call(function() {
    (new MiscController())->testRenderView();
})->daily()->at('13:00');

Scenario 1: If I navigate to 127.0.0.1:8000/testRenderView, it's working and file rendered_view.php is saved in public folder with expected content:

<?
$someVariable = "123456";
require_once("includes/php_file.php");
?>

Scenario 2: If It's executed by scheduler (at 13:00), it returns error:

local.ERROR: Illuminate\View\Engines\PhpEngine::main(): Failed opening required 'includes/php_file.php' (include_path='.:') {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 64): Illuminate\View\Engines\PhpEngine::main(): Failed opening required 'includes/php_file.php' (include_path='.:')

Looks like when it's executed from Scheduler, Laravel tries to render the view as real view. I also tried to create artisan commands, but the behaviour is the same. Works fine when I execute the command on console, but doesn't when I call the command from Scheduler. Any idea why it's happening?

Denis
  • 170
  • 1
  • 9

1 Answers1

0

Instead of creating a new controller like (new MiscController())->testRenderView();, just do your logic here and replace it with file_put_contents(resource_path('myfile.php'), view('myview')->render()).

Ive just tested it and it works.

You shouldnt try to create controllers in the scheduler just like you did, because they ideally need some sort of request. You are better off with just creating a new class with some functions.

Flame
  • 6,663
  • 3
  • 33
  • 53
  • I have never used this function. But I tried to make it like `ob_start(); view('view_to_render', ['variable' => '123456']); $result = ob_get_clean(); file_put_contents(public_path('rendered_view.php'), $result); ob_end_clean();` and it doesn't work – Denis Jan 16 '19 at 10:11
  • i've changed the answer. Forget the `ob_clean` stuff since your given code almost works by itself already – Flame Jan 17 '19 at 11:37