Post by PeteA » Mon Sep 08, 2014 4:40 pm

Is there any way to access the $this->data variable from outside the controller it was declared within? I know the PHP limitations on variable scope but wondered if there was anything within OpenCart that facilitated this.

My reason is that I want to access the breadcrumb trail within the header (for passing metadata using LD JSON), the most reliable way of doing this would be to just tap into the breadcrumbs of the page and go from there. I don't really want to mod every single controller to pass the breadcrumb information if I can avoid it :D

New member

Posts

Joined
Wed Jul 30, 2014 5:46 pm

Post by MarketInSG » Mon Sep 08, 2014 9:15 pm

You can instead try rebuilding the breadcrumb URL in the header as you require. It shouldn't be difficult to fetch the current URL the user is on.


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by PeteA » Mon Sep 08, 2014 9:21 pm

MarketInSG wrote:You can instead try rebuilding the breadcrumb URL in the header as you require. It shouldn't be difficult to fetch the current URL the user is on.
Sadly that wouldn't work as I've been forced to use a predefined format for URLs that doesn't contain the pathing information. The actual breadcrumbs are rebuilt on the fly for categories and products

New member

Posts

Joined
Wed Jul 30, 2014 5:46 pm

Post by MarketInSG » Mon Sep 08, 2014 9:33 pm

How are you rebuilding your URLs? If you're using SEO URL function, you can still fetch the route with $this->request->get['route']


User avatar
Guru Member

Posts

Joined
Wed Nov 16, 2011 11:53 am
Location - Singapore

Post by PeteA » Mon Sep 08, 2014 11:16 pm

I'm using mods into various files. Basically all product urls are [productname].html and all categories consist of just a single category (even nested ones). It's legacy from our current website so sadly nothing that can be done.

It looks like I might have vQmod a line in before the various Controllers perform the render to screen and pass $this->data to a method that pulls out the breadcrumbs (and any other data too).

New member

Posts

Joined
Wed Jul 30, 2014 5:46 pm

Post by PeteA » Mon Sep 08, 2014 11:27 pm

MarketInSG wrote:How are you rebuilding your URLs? If you're using SEO URL function, you can still fetch the route with $this->request->get['route']
Just re-read this. I'm using $this->request->get['route'] and $this->request->get['path'] to rebuild the breadcrumbs for products and categories so that I accurately display the correct breadcrumb data at the top of each page (works like a charm).

Where the problem occurs is that I need this information within the header template (thus Controller) as breadcrumbs on'y exist within the controller where they're generated (catalog/controller/category and catalog/controller/product). I could just reuse this logic, but that's going to be very inefficient and only work for category and product pages where as I want the grab the breadcrumb data for any and all pages.

New member

Posts

Joined
Wed Jul 30, 2014 5:46 pm

Post by PeteA » Tue Sep 09, 2014 3:22 pm

Just an update that I've managed to solve this although I'm happy for somebody to suggest a better solution ;)

End result has been to create a vQmod that adds some code within /system/engine/controller.php as the very first call of the render() method. This queries if $this->data['breadcrumbs'] exists and if so then passed that data to $this->document that has been extended to handle the LD+JSON components.

New member

Posts

Joined
Wed Jul 30, 2014 5:46 pm

Post by webshop » Sun May 26, 2019 12:59 am

Hello all,

How can I access one variable from an extension in header.twig?

You can see my example here how I managed to access variable from setting (controller and view) files in header.
https://github.com/bblori/Enable-Style-OC3

But this time I want to manage from an extension, I inserted the same code in extension admin files (controller and view) but is not working, what else need to be added?

Thanks,

Newbie

Posts

Joined
Mon May 20, 2019 11:42 pm

Post by diegospm » Fri May 31, 2019 8:49 am

PeteA wrote:
Tue Sep 09, 2014 3:22 pm
Just an update that I've managed to solve this although I'm happy for somebody to suggest a better solution ;)

End result has been to create a vQmod that adds some code within /system/engine/controller.php as the very first call of the render() method. This queries if $this->data['breadcrumbs'] exists and if so then passed that data to $this->document that has been extended to handle the LD+JSON components.
Hi,

If you still have an interest, maybe my pull request might help you: https://github.com/opencart/opencart/pull/7398

Initially I created the breadcrumb class to be used in administration, but I will extend it to the catalog as well, when I have a free time

You can use the logic behind this class for your project:

Create an event before in the header class and then pass the variable route and data. Then create a controller file wherever you want (I suggest in the common folder). In this file you can capture the route and add to the breadcrumbs array. Add the LD + JSON information you need.

Finally, you render in the twig file. That way you do not need Vqmod / Ocmod and it does not change the original OC files.
Last edited by diegospm on Fri May 31, 2019 11:21 am, edited 1 time in total.

Newbie

Posts

Joined
Thu Apr 18, 2019 8:49 am

Post by diegospm » Fri May 31, 2019 8:55 am

webshop wrote:
Sun May 26, 2019 12:59 am
Hello all,

How can I access one variable from an extension in header.twig?

You can see my example here how I managed to access variable from setting (controller and view) files in header.
https://github.com/bblori/Enable-Style-OC3

But this time I want to manage from an extension, I inserted the same code in extension admin files (controller and view) but is not working, what else need to be added?

Thanks,
Hi,

With events you can change the keys of the data variable, however you can not change variables of type string, object, etc, even if they are within the scope of the method (except for the route).

Newbie

Posts

Joined
Thu Apr 18, 2019 8:49 am
Who is online

Users browsing this forum: No registered users and 6 guests