Page 1 of 1

Get logged in customer in view

Posted: 14 Oct 2019, 12:58
by MikaelNazarenko
Hi aimeos community, tell me, please, how to get current logged in aimeos customer object in view ? Should I extend something for this ?

Re: Get logged in customer in view

Posted: 14 Oct 2019, 15:48
by aimeos
You have to create a HTML client decorator that add the customer object to the view:
https://aimeos.org/docs/Developers/Html ... components

You can get the customer item of the logged in user with:

Code: Select all

$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->get();

Re: Get logged in customer in view

Posted: 14 Oct 2019, 16:17
by MikaelNazarenko
Ok, thank you! But if I will do

Code: Select all

$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->get();
, in this case I will duplicate the query to fetch user ? Because I think it is already fetched

Re: Get logged in customer in view

Posted: 14 Oct 2019, 16:38
by aimeos
No, it isn't. Only the user ID is available in the context but the item hasn't been retrieved until you create the frontend controller.

Re: Get logged in customer in view

Posted: 19 Nov 2019, 09:20
by MikaelNazarenko
The little problem is that I need user object in a lot of partials. Then I have to find all places where partials are included and pass user to them.

Can I make it simply somehow ? Make it automatically or I don't know ) then I can use user in all partials )

Re: Get logged in customer in view

Posted: 19 Nov 2019, 12:42
by MikaelNazarenko
I have solved it ) Maybe will be useful for somebody:

ext/labor/client/html/src/Client/Helper/Partial/Partial.php :

Code: Select all

<?php

namespace Aimeos\Client\Helper\Partial;

class Partial extends \Aimeos\MW\View\Helper\Partial\Standard
{

    /**
     * @param string $file
     * @param array $params
     * @return string
     * @throws \Aimeos\MW\View\Exception
     */
    public function transform( $file, array $params = [] )
    {
        $view = clone $this->getView();
        $params['user'] = $view->user;
        $view->assign( $params );

        return $view->render( $file );
    }
}
ext/labor/client/html/src/Client/Html/Common/Decorator/CustomDecorator.php :

Code: Select all

<?php

namespace Aimeos\Client\Html\Common\Decorator;

class CustomDecorator extends Base
{
    public function addData( \Aimeos\MW\View\Iface $view, array &$tags = array(), &$expire = null )
    {
        $view = parent::addData( $view, $tags, $expire );

        $view->user = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' )->get();

        $helper = new \Aimeos\Client\Helper\Partial\Partial($view);

        $view->addHelper('partial', $helper);

        return $view;
    }
}

Then you will have user object in all partials )

Re: Get logged in customer in view

Posted: 19 Nov 2019, 13:08
by MikaelNazarenko
aimeos wrote: 14 Oct 2019, 15:48 You have to create a HTML client decorator that add the customer object to the view:
https://aimeos.org/docs/Developers/Html ... components

You can get the customer item of the logged in user with:

Code: Select all

$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->get();

Code: Select all

$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->get();
Also as I see the above code will not return current logged in user. It will return empty user, because it is

https://github.com/aimeos/ai-controller ... rd.php#L53

Seems it should look like this:

Code: Select all

$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->uses([])->get();