简体   繁体   中英

How to get the current logged-in user from Fluid?

I need a way to get the current logged in user object from Fluid. I need it in Partials and Layouts, not only Templates, so I can't do this by getting it as an argument from a Controller class.

I already know how to do this from Typoscript, eg to get his/her name:

TSFE:fe_user|user|name

To get it from Php I use my own static function from my own extended FrontendUser class: User, called User::getLogged() . Could I call this function from a Partial? Or is there any other method?

Thanks in advance!

TYPO3 10 Tutorial

First of all, if you want to use your partials then you can call them via Templates layouts etc. You can give them some arguments as well. So if you want to call for example the menu partial you could do something like that if you want to pass some arguments:

<f:render partial="Menus/Menu" arguments="{user: user}"/>

or if you want to pass all the arguments

<f:render partial="Menus/Menu" arguments="{_all}"/>

Now if you are using your controller to get the user object there are better ways to do get your user in FrontEnd. You can use the TYPO3 Context API for that. You can build a ViewHelper for that which you can call it wherever you want.

First, you need to register your ViewHelper. Under your_extension/ext_localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['yourNamespace'] = ['YourVendor\\YourExtension\\ViewHelpers'];

Then under your_extension/Classes/ViewHelpers/Data/UserViewHelper.php

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;


class UserViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    public static function renderStatic(array $arguments,\Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)  
    {
       $output = $renderChildrenClosure();
       $userRepository = GeneralUtility::makeInstance(FrontendUserRepository::class);
       $context = GeneralUtility::makeInstance(Context::class);
       $userId = $context->getPropertyFromAspect('frontend.user', 'id');
       $user = $userRepository->findByUid($userId);
       $renderingContext->getVariableProvider()->add('loggedInUser',$user);
       
       return $output;
}

Then on your Templates/Partials/Layouts somewhere you can do the following:

<yourNamespace:data.user/>

And if you debug your variables <f:debug>{_all}</f:debug> then you will that you have a new variable called loggedInUser and in there is your user object. You can use it wherever you want and get all the information you need.

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM