0

I have a Laravel 9 (latest) site which is throwing an exception when rendering core 404 blade.

Exception: htmlspecialchars(): Argument #1 ($string) must be of type string, _ given .../var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/404.blade.php

404.blade.php

@extends('errors::minimal')

@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))

If I change __('Not Found') to 'Not Found', the exception is not thrown and the 404 page renders correctly.

This is not an issue on my local environment, but I'm using the same PHP version 8.1.22.

UPDATE: Trace...

#0 /var/app/current/vendor/laravel/framework/src/Illuminate/Support/helpers.php(123): htmlspecialchars(Object(_), 3, 'UTF-8', true)
#1 /var/app/current/vendor/laravel/framework/src/Illuminate/View/Concerns/ManagesLayouts.php(53): e(Object(_))
#2 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/404.blade.php(3): Illuminate\\View\\Factory->startSection('title', Object(_))
#3 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(109): require('/var/app/curren...')
#4 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(110): Illuminate\\Filesystem\\Filesystem::Illuminate\\Filesystem\\{closure}()
#5 /var/app/current/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(58): Illuminate\\Filesystem\\Filesystem->getRequire('/var/app/curren...', Array)

TIA

1
  • Is __('Not Found') for translation? Why not use a variable in blade? @php $not_found = __('Not Found'); @endphp @section('title', $not_found) Commented Sep 26, 2023 at 1:11

1 Answer 1

1

Did you try using variable?

@php
    $not_found = __('Not Found');
@endphp
@section('title', $not_found)

It is looking for plain string.

Not sure why the 2nd param of @section does not accept the translation method: __('Not Found') Based on the documentation, it accepts string. So I suggested to use a variable instead of the translation method.

Sign up to request clarification or add additional context in comments.

4 Comments

how does it make any difference?
@OMiShah updated my answer
Thanks for the responses. This is one of the core exception handler templates. I can override, but I'm trying to determine why __('...') is not returning a string or not being parsed correctly as it is probably indicative of another issue.
@bfuzze Most probably before the @section recognize the string __('Not Found'), they see the _ and giving error not recognizing this method. Not 100% sure but it might be a reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.