EDIT: The code snippet below, where view takes an array ['template' => 'my template'] as the first argument is a feature of wpb/string-blade-compiler which is overriding the native laravel functionality.
I have a directive registered in AppServiceProvider::boot:
public function boot()
{
Blade::directive('hello', function($expression) {
return "<?php echo 'Hello world'; ?>";
});
}
It works perfectly when I use file based templates saved as resources/views/something.blade.php and used return view('something', $data) in my Controller::action.
However when I try:
try {
return view(['template' => $template], $data)->render();
} catch(\ErrorException $ex) {
preg_match('/Undefined variable: (.+?)\s/', $ex->getMessage(), $matches);
if ($matches) {
return sprintf('Template: variable {{ $%s }} is invalid', $matches[1]);
}
return sprintf('%s: %s', $attribute, $ex->getMessage());
}
And try and use templates from a string, the directive isn't loaded. No errors, no nothing.
Does anyone with an intimate knowledge of laravel know the difference with these two contexts? I would have thought they'd yield the same result but do not. I'm struggling to understand the laravel architecture to unravel this one. Thanks.
composer.json:
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"laravelcollective/html": "^5.2",
"maatwebsite/excel": "^2.1",
"sofa/eloquence": "^5.2",
"wpb/string-blade-compiler": "^3.2",
"doctrine/dbal": "^2.5",
"davejamesmiller/laravel-breadcrumbs": "^3.0"
}
PHP 5.6
View::make()instead?