How to use Laravel function in own PHP file? #4303

Closed
opened 2026-02-05 08:29:27 +03:00 by OVERLORD · 7 comments
Owner

Originally created by @ikabod on GitHub (Nov 7, 2023).

Attempted Debugging

  • I have read the debugging page

Searched GitHub Issues

  • I have searched GitHub for the issue.

Describe the Scenario

Hello,

a PHP file in the public folder of BookStack contains the following content:

<?php

    // Register The Auto Loader
    require __DIR__ . '/../vendor/autoload.php';

    // Output a translation string
    echo trans('common.back_to_top');
   
?>

When the file is called, it throws the following error:


Fatal error: Uncaught ReflectionException: Class "translator" does not exist in /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php:889 Stack trace: #0 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(889): ReflectionClass->__construct() #1 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container->build() #2 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Container\Container->resolve() #3 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Container\Container->make() #4 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(938): app() #5 /var/www/bookstack/public/wiki.php(10): trans() #6 {main}

Next Illuminate\Contracts\Container\BindingResolutionException: Target class [translator] does not exist. in /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php:891
Stack trace:
#0 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container->build()
#1 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Container\Container->resolve()
#2 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Container\Container->make()
#3 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(938): app()
#4 /var/www/bookstack/public/wiki.php(10): trans()
#5 {main}
thrown in /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 891

What is missing in the PHP file?

Exact BookStack Version

v23.10

Log Content

No response

Hosting Environment

PHP 8.2 on a local system

Originally created by @ikabod on GitHub (Nov 7, 2023). ### Attempted Debugging - [X] I have read the debugging page ### Searched GitHub Issues - [X] I have searched GitHub for the issue. ### Describe the Scenario Hello, a PHP file in the public folder of BookStack contains the following content: ``` <?php // Register The Auto Loader require __DIR__ . '/../vendor/autoload.php'; // Output a translation string echo trans('common.back_to_top'); ?> ``` When the file is called, it throws the following error: <br /> <b>Fatal error</b>: Uncaught ReflectionException: Class &quot;translator&quot; does not exist in /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php:889 Stack trace: #0 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(889): ReflectionClass-&gt;__construct() #1 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container-&gt;build() #2 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Container\Container-&gt;resolve() #3 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Container\Container-&gt;make() #4 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(938): app() #5 /var/www/bookstack/public/wiki.php(10): trans() #6 {main} Next Illuminate\Contracts\Container\BindingResolutionException: Target class [translator] does not exist. in /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php:891 Stack trace: #0 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(770): Illuminate\Container\Container-&gt;build() #1 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php(706): Illuminate\Container\Container-&gt;resolve() #2 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(120): Illuminate\Container\Container-&gt;make() #3 /var/www/bookstack/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(938): app() #4 /var/www/bookstack/public/wiki.php(10): trans() #5 {main} thrown in <b>/var/www/bookstack/vendor/laravel/framework/src/Illuminate/Container/Container.php</b> on line <b>891</b><br /> What is missing in the PHP file? ### Exact BookStack Version v23.10 ### Log Content _No response_ ### Hosting Environment PHP 8.2 on a local system
OVERLORD added the 🐕 Support label 2026-02-05 08:29:27 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Nov 7, 2023):

Hi @ikabod,

Attempting to use the app internals externally is really not something I can recommend, so I'm hesitant to spend time in supporting it here.

Can you explain what you're attempting to achieve?
The logical theme system may allow you to achieve the same goal which within you'd have full access to app internal functions.

@ssddanbrown commented on GitHub (Nov 7, 2023): Hi @ikabod, Attempting to use the app internals externally is really not something I can recommend, so I'm hesitant to spend time in supporting it here. Can you explain what you're attempting to achieve? The logical theme system may allow you to achieve the same goal which within you'd have full access to app internal functions.
Author
Owner

@ikabod commented on GitHub (Nov 7, 2023):

Hi Dan,

there is a small web page with some elements and an iframe. In the iframe BookStack is running.

Some of the elements of the web page should be multilingual and translated during the page load.

Since Laravel is already installed, I thought it is possible to use their functionality?

The php file (name it app.php) is loading the autoloader, in the same way as in the index.php file (both are on the same directory). So I'm a bit confused why it is working in this file but not in another?

Or to be more precise: Why it is loading some classes, but do not find the class "translator"?

Thanks,
Peter

@ikabod commented on GitHub (Nov 7, 2023): Hi Dan, there is a small web page with some elements and an iframe. In the iframe BookStack is running. Some of the elements of the web page should be multilingual and translated during the page load. Since Laravel is already installed, I thought it is possible to use their functionality? The php file (name it **app.php**) is loading the autoloader, in the same way as in the index.php file (both are on the same directory). So I'm a bit confused why it is working in this file but not in another? Or to be more precise: Why it is loading some classes, but do not find the class "translator"? Thanks, Peter
Author
Owner

@ssddanbrown commented on GitHub (Nov 7, 2023):

Okay, I don't understand though why you don't built this as an app endoint where all the functionality you desire is already loaded? Instead of trying to replicate the app environment.

Or to be more precise: Why it is loading some classes, but do not find the class "translator"?

Because a lot of functionality is resolved through the app's containers, and the app instance registers and manages the classes to be resolved from the container for various parts of functionality.

In this case, when trans() is called that tries to resolve translator for the container but that has not been registered by this point.

@ssddanbrown commented on GitHub (Nov 7, 2023): Okay, I don't understand though why you don't built this as an app endoint where all the functionality you desire is already loaded? Instead of trying to replicate the app environment. > Or to be more precise: Why it is loading some classes, but do not find the class "translator"? Because a lot of functionality is resolved through the app's containers, and the app instance registers and manages the classes to be resolved from the container for various parts of functionality. In this case, when `trans()` is called that tries to resolve `translator` for the container but that has not been registered by this point.
Author
Owner

@ikabod commented on GitHub (Nov 7, 2023):

Can you please let me know how to register the class translator?

I already have a logical theme in use with a folder "lang" to translate stuff, which I use inside BookStack.

If I can use this folder for the couple of strings for my application running outside, it would make my life much easier.

@ikabod commented on GitHub (Nov 7, 2023): Can you please let me know how to register the class `translator`? I already have a logical theme in use with a folder "lang" to translate stuff, which I use inside BookStack. If I can use this folder for the couple of strings for my application running outside, it would make my life much easier.
Author
Owner

@ssddanbrown commented on GitHub (Nov 7, 2023):

This is really getting outside the bounds of what I'm willing to support to be honest, and into general PHP/Laravel learning.
From my point of view, you're trying to do things in a more difficult way then I'm advising while asking for my time to support that more difficult path.

If you really want to go down that path, then I would advise that you explore how the app instance is built (starting from public/index.php and how the app registers and manages services/classes in it's container).
BookStack does do register the translator via it's own provider, to change some of the behaviour of a default Laravel setup. This can be seen here:
295cd01605/app/App/Providers/TranslationServiceProvider.php

Ultimately, this is all linked into the app instance, container and config options of BookStack. If you expect to use user language preferences, that's another layer on top.

@ssddanbrown commented on GitHub (Nov 7, 2023): This is really getting outside the bounds of what I'm willing to support to be honest, and into general PHP/Laravel learning. From my point of view, you're trying to do things in a more difficult way then I'm advising while asking for my time to support that more difficult path. If you really want to go down that path, then I would advise that you explore how the app instance is built (starting from `public/index.php` and how the app registers and manages services/classes in it's container). BookStack does do register the translator via it's own provider, to change some of the behaviour of a default Laravel setup. This can be seen here: https://github.com/BookStackApp/BookStack/blob/295cd0160525125bbd7756d7ad07392ae7201cb8/app/App/Providers/TranslationServiceProvider.php Ultimately, this is all linked into the app instance, container and config options of BookStack. If you expect to use user language preferences, that's another layer on top.
Author
Owner

@ikabod commented on GitHub (Nov 9, 2023):

"From my point of view, you're trying to do things in a more difficult way then I'm advising while asking for my time to support that more difficult path."

Maybe your view is wrong, but I totally understand if you do not want to give support if you think this way.

Do you mind if I post a solution for the described scenario if I find any?

@ikabod commented on GitHub (Nov 9, 2023): "_From my point of view, you're trying to do things in a more difficult way then I'm advising while asking for my time to support that more difficult path._" Maybe your view is wrong, but I totally understand if you do not want to give support if you think this way. Do you mind if I post a solution for the described scenario if I find any?
Author
Owner

@ssddanbrown commented on GitHub (Nov 9, 2023):

Do you mind if I post a solution for the described scenario if I find any?

Sure! Otherwise I'll go ahead and close this off.

@ssddanbrown commented on GitHub (Nov 9, 2023): > Do you mind if I post a solution for the described scenario if I find any? Sure! Otherwise I'll go ahead and close this off.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#4303