Get logged in customer in view
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Get logged in customer in view
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
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:
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, give us a star
If you like Aimeos, give us a star
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: Get logged in customer in view
Ok, thank you! But if I will do , in this case I will duplicate the query to fetch user ? Because I think it is already fetched
Code: Select all
$view->user = \Aimeos\Controller\Frontend::create( $context, 'customer' )->get();
Re: Get logged in customer in view
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, give us a star
If you like Aimeos, give us a star
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: Get logged in customer in view
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 )
Can I make it simply somehow ? Make it automatically or I don't know ) then I can use user in all partials )
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: Get logged in customer in view
I have solved it ) Maybe will be useful for somebody:
ext/labor/client/html/src/Client/Helper/Partial/Partial.php :
ext/labor/client/html/src/Client/Html/Common/Decorator/CustomDecorator.php :
Then you will have user object in all partials )
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 );
}
}
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 )
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: Get logged in customer in view
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();
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();