Generate Product URL in Laravel Controller
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!
-
- Posts: 8
- Joined: 04 Jan 2020, 04:47
Generate Product URL in Laravel Controller
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
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
}
}
Re: Generate Product URL in Laravel Controller
Retrieve the Aimeos context object first. It contains everything you need including the view:
https://aimeos.org/docs/Laravel/Extend_Aimeos
Code: Select all
$context = $this->app->make('\Aimeos\Shop\Base\Context')->get();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

-
- Posts: 8
- Joined: 04 Jan 2020, 04:47
Re: Generate Product URL in Laravel Controller
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:
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
Re: Generate Product URL in Laravel Controller
The context has a getView() and getConfig() method besides others:
https://github.com/aimeos/aimeos-core/b ... e.php#L235
Code: Select all
$config = $context->getConfig();
$view = $context->getView();
$target = $config->get( 'client/html/catalog/detail/url/target' );
$url = $view->url( $target, ... );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

-
- Posts: 8
- Joined: 04 Jan 2020, 04:47
Re: Generate Product URL in Laravel Controller
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
Re: Generate Product URL in Laravel Controller
Sorry, my fault 
The view must be instantiated separately:

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,
give us a star
If you like Aimeos,

- parmonov98
- Posts: 33
- Joined: 24 Sep 2020, 12:12
Re: Generate Product URL in Laravel Controller
I'm getting an error like this.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() );
https://i.imgur.com/wUu0Yay.png
Re: Generate Product URL in Laravel Controller
This has been changed to ::create():
https://github.com/aimeos/aimeos-larave ... hp#L66-L87
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,
give us a star
If you like Aimeos,

Re: Generate Product URL in Laravel Controller
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' )
]);