stackprogramer's avatar

trying to include a functions.php file from my resource folder within a service provider

when i add a php source with require/include once (a functions.php file) in service provider,I have strange error.....my name and path is completely true but i can not include a php file from resource folder. I need it for a special case. Thanks in advance

Snippet code

$theme = getTheme(); // returns something like 'front'
    //$themePath = base_path("resources/themes/front/functions.php");
    $themePath = realpath(__DIR__ . '/../resources/themes/front/functions.php');
    require_once $themePath;

    if (file_exists($themePath)) {
        require_once $themePath;
    }


Error

resources/front/functions.php): Failed to open stream: No such file or directory
0 likes
6 replies
JussiMannisto's avatar

You must have the wrong path somewhere. Your code shows this path:

resources/themes/front/functions.php

But the error message shows this path:

resources/front/functions.php

I don't know why you're writing dynamic imports like this, though. Whatever you're trying to do, there's probably a better approach.

1 like
Snapey's avatar

why can you not move the file or its contents into the provider?

If you are trying to run a file that is actually in the public folder (or a child) then you are introducing a big security risk.

use

require_once(base_path(resources/themes/front/functions.php));
1 like
martinbean's avatar

@stackprogramer I think you’re thinking about this in entirely the wrong way.

Why is a service provider trying to import a functions file from a “theme”? Your theme should have a service provider class that registers anything that theme needs to function.

Please or to participate in this conversation.