luceos I know this is an old thread, but it is exactly what I am trying to achieve.
I Need to get the current user in a Formatter
extension:
// extend.php
return [
(new Extend\Formatter)
->configure(CrossReferencesConfigurator::class)
->render(CrossReferencesRenderer::class),
...
]
Ideally, I would need it in both the filterCrossReference
function of the CrossReferenceConfigurator
and the CrossReferenceRenderer
, in order to enforce some permissions with $user->can()
. In the former to invalidate the tag if the actor does not have the permission to read the target discussion, and in the latter to hide the title of the target discussion if the reader does not have this permission.
I started with the renderer as it seemed easier, but did not manage to do it. Itried to autowire a Psr\Http\Message\ServerRequestInterface
in the constructor but I got the same error as msyadav above:
flarum.ERROR: Illuminate\Contracts\Container\BindingResolutionException: Target [Psr\Http\Message\ServerRequestInterface] is not instantiable. in /home/nicolas/Source/www/flarum/vendor/illuminate/container/Container.php:1089
I then tried the Laravel way with Auth::user()
but got the following error:
flarum.ERROR: RuntimeException: A facade root has not been set. in /home/nicolas/Source/www/flarum/vendor/illuminate/support/Facades/Facade.php:258
So what is the correct way to get the current Flarum user in PHP in 2023? Ideally it would be similar to the Laravel way, i.e. with a static method that I can call almost anywhere, as otherwise I am not sure how I could get it in the filter function.