Skip to content

Call to undefined function #144

@silverqx

Description

@silverqx

Hi, I need a help, am getting this error:

In app.php line 34:
                                                                              
Call to undefined function App\Common\Core\Support\shouldRespondWithJson()

and in composer.json

    "autoload": {
        "files": [
            "app/Common/Core/Support/functions.php"
        ],
    },

app/Common/Core/Support/functions.php looks like:

<?php

namespace App\Common\Core\Support;

use Throwable;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

if (!function_exists('App\Common\Core\Support\shouldRespondWithJson')) {
    /**
     * Determine if the exception should be rendered as JSON.
     *
     * In production, always return JSON.
     * In non-production, return JSON unless the developer explicitly disables it
     * via the `disable-json` query parameter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $e
     * @return bool
     */
    function shouldRespondWithJson(Request $request, Throwable $e): bool
    {
        $isProduction = app()->isProduction();
        $forceJson = $isProduction || !$request->boolean('disable-json', false);

        if (!$isProduction)
            Log::debug('Exception rendering as ' . ($forceJson ? 'JSON' : 'HTML'), [
                'url' => $request->fullUrl(),
                'exception' => get_class($e),
            ]);

        /* Always return JSON in production, or if the developer did not explicitly disable it.
               In non-production, use the default logic to determine if the request expects JSON. */
        return $forceJson || $request->expectsJson();
    }
}

And then am calling it in bootstrap/app.php like:

use function App\Common\Core\Support\shouldRespondWithJson;

return Application::configure(basePath: dirname(__DIR__))
    ...
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->shouldRenderJsonWhen(shouldRespondWithJson(...));
    })
    ->create();

Is there any way I can tell Laravel Extra where to find this function?

thx for any advice

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions