Get logged in customer in view

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
MikaelNazarenko
Advanced
Posts: 167
Joined: 27 Jun 2019, 16:19

Get logged in customer in view

Post by MikaelNazarenko » 14 Oct 2019, 12:58

Hi aimeos community, tell me, please, how to get current logged in aimeos customer object in view ? Should I extend something for this ?

User avatar
aimeos
Administrator
Posts: 4137
Joined: 01 Jan 1970, 00:00

Re: Get logged in customer in view

Post by aimeos » 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();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Advanced
Posts: 167
Joined: 27 Jun 2019, 16:19

Re: Get logged in customer in view

Post by MikaelNazarenko » 14 Oct 2019, 16:17

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

User avatar
aimeos
Administrator
Posts: 4137
Joined: 01 Jan 1970, 00:00

Re: Get logged in customer in view

Post by aimeos » 14 Oct 2019, 16:38

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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Advanced
Posts: 167
Joined: 27 Jun 2019, 16:19

Re: Get logged in customer in view

Post by MikaelNazarenko » 19 Nov 2019, 09:20

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 )

MikaelNazarenko
Advanced
Posts: 167
Joined: 27 Jun 2019, 16:19

Re: Get logged in customer in view

Post by MikaelNazarenko » 19 Nov 2019, 12:42

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 )

MikaelNazarenko
Advanced
Posts: 167
Joined: 27 Jun 2019, 16:19

Re: Get logged in customer in view

Post by MikaelNazarenko » 19 Nov 2019, 13:08

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();

Post Reply