Generate Product URL in Laravel Controller

Help for integrating the Laravel package
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!
VanCityDev
Posts: 8
Joined: 04 Jan 2020, 04:47

Generate Product URL in Laravel Controller

Post by VanCityDev » 04 Jan 2020, 05:01

Created a laravel controller so I can have a custom page for a specific category listing. Cannot figure out how to generate the Product URL on this page (ie: the URL function). The below obviosly doesn't work, as I don't have access to the $view variable in the laravel controller.

Laravel: 6.9
Aimeos: 2019.10
PHP: 7.2.25

Code: Select all

<?php

namespace App\Http\Controllers;
 
use Aimeos\Shop\Facades\Shop;

class ProductsController extends Controller
{
    public function indexAction()
    {
		
		//doing some stuff here
		
		
		$target = config( 'client/html/catalog/detail/url/target' );
		$controller = config( 'client/html/catalog/detail/url/controller', 'catalog' );
		$action = config( 'client/html/catalog/detail/url/action', 'detail' );
		$config = config( 'client/html/catalog/detail/url/config', array() );
		$trailing = array( 'anchor' ); // will be added as #anchor
		$params = array( 'd_prodid' => 1 );

		$test_url = $view->url( $target, $controller, $action, $params, $trailing, $config );  //this gives me undefined var $view
		
		
		//doing some other stuff here prior to sending to view
		
		
    }
}

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

Re: Generate Product URL in Laravel Controller

Post by aimeos » 04 Jan 2020, 10:36

Retrieve the Aimeos context object first. It contains everything you need including the view:

Code: Select all

$context = $this->app->make('\Aimeos\Shop\Base\Context')->get();
https://aimeos.org/docs/Laravel/Extend_Aimeos
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

VanCityDev
Posts: 8
Joined: 04 Jan 2020, 04:47

Re: Generate Product URL in Laravel Controller

Post by VanCityDev » 04 Jan 2020, 18:56

Thanks for the reply, still not sure though.

I can retrieve the aimeos context object like so, but I cannot figure out how to get the view / Url function from that.

This does not work:

Code: Select all

                $context = app( 'aimeos.context' )->get(); //get the context object successfully 
		
		$view = $context->view(); //this fails, undefined method
		
		$url = $context->url(...insert vars here...); //this fails, undefined method

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

Re: Generate Product URL in Laravel Controller

Post by aimeos » 05 Jan 2020, 09:51

The context has a getView() and getConfig() method besides others:

Code: Select all

$config = $context->getConfig();
$view = $context->getView();
$target = $config->get( 'client/html/catalog/detail/url/target' );
$url = $view->url( $target, ... );
https://github.com/aimeos/aimeos-core/b ... e.php#L235
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

VanCityDev
Posts: 8
Joined: 04 Jan 2020, 04:47

Re: Generate Product URL in Laravel Controller

Post by VanCityDev » 06 Jan 2020, 04:47

Ok. So when I try to call getView() from my controller I get an error:

Code: Select all

$context = app( 'aimeos.context' )->get(); //this works
$config = $context->getConfig(); //this works
$view = $context->getView(); //this generates the error

Code: Select all

Aimeos\MShop\Exception
View object not available

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

Re: Generate Product URL in Laravel Controller

Post by aimeos » 07 Jan 2020, 13:03

Sorry, my fault :-(
The view must be instantiated separately:

Code: Select all

$context = app( 'aimeos.context' )->get();
$tmplPaths = app( 'aimeos' )->get()->getCustomPaths( 'client/html/templates' );
$view = app( 'aimeos.view' )->get( $context, $tmplPaths, $context->getLocale()->getLanguageId() );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
parmonov98
Posts: 33
Joined: 24 Sep 2020, 12:12

Re: Generate Product URL in Laravel Controller

Post by parmonov98 » 10 Oct 2020, 09:41

aimeos wrote: 07 Jan 2020, 13:03 Sorry, my fault :-(
The view must be instantiated separately:

Code: Select all

$context = app( 'aimeos.context' )->get();
$tmplPaths = app( 'aimeos' )->get()->getCustomPaths( 'client/html/templates' );
$view = app( 'aimeos.view' )->get( $context, $tmplPaths, $context->getLocale()->getLanguageId() );
I'm getting an error like this.
https://i.imgur.com/wUu0Yay.png

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

Re: Generate Product URL in Laravel Controller

Post by aimeos » 11 Oct 2020, 08:16

This has been changed to ::create():
https://github.com/aimeos/aimeos-larave ... hp#L66-L87

Code: Select all

$context = app( 'aimeos.context' )->get();
$tmplPaths = app( 'aimeos' )->get()->getCustomPaths( 'client/html/templates' );
$view = app( 'aimeos.view' )->create( $context, $tmplPaths, $context->getLocale()->getLanguageId() );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
peter69
Posts: 95
Joined: 09 Jun 2022, 19:31

Re: Generate Product URL in Laravel Controller

Post by peter69 » 17 Feb 2023, 05:48

In case someone finds it useful, to obtain the URL of a category in a Controller, you can do it in the following way:

Code: Select all

$context = app('aimeos.context')->get();
$manager = \Aimeos\MShop::create( $context, 'catalog' );

$catalog_code = 'home';
$category = $manager->find($catalog_code, ['text']);

$url = $context->view()->link( 'client/html/catalog/tree/url', [
            'f_catid' => $category->getId(),
            'f_name' => $category->getName( 'url' )
]);

Post Reply