Post by twisted1919 » Wed Mar 28, 2012 11:14 pm

After i did a quick search on this very topic, i couldn't find anything in place to help me with the issue.
My problem is that, i always need to change the base controllers in order to adjust the store to the requirements and this is a nightmare when upgrading, i mean, usually you just can't upgrade because there are too many changes to keep track of.
However, as i've read, OC doesn't currently support a way to extend the base controllers, so i modified the action class to accomplish this thing.

Code: Select all

	public function __construct($route, $args = array()) {
		// run default implementation
        $this->routeToFile($route, $args);
        
        //if we found a base controller, we will include it, 
        //so that later we may extend it, or just override it completely.
        if(!empty($this->file)&&is_file($this->file)){
            require_once($this->file);    
        }
        //store the current values in a variable so that we can assign them back later if it's the case,
        //if there is no custom implementation of our current controller, and reset each property of the object.
        $props=get_object_vars($this);
        foreach($props AS $key=>$value){
           $this->$key=null;
        }
        
        //look for a custom controller.
        $this->routeToFile($route, $args, 'custom_controller','Custom');
        
	    //if there is no file, means we did not find a custom controller,
        //so we assign the base controller values back.
        if(empty($this->file)||!is_file($this->file)){
           foreach($props AS $key=>$value){
	           $this->$key=$value;
	       }
	    }   
    }
    
   	protected function routeToFile($route, $args = array(), $controllerFolder='controller', $classPrefix='') {
		$path = '';
		
		$parts = explode('/', str_replace('../', '', (string)$route));
		
		foreach ($parts as $part) { 
			$path .= $part;
			
			if (is_dir(DIR_APPLICATION . $controllerFolder. '/' . $path)) {
				$path .= '/';
				
				array_shift($parts);
				
				continue;
			}
			
			if (is_file(DIR_APPLICATION . $controllerFolder. '/' . str_replace('../', '', $path) . '.php')) {
				$this->file = DIR_APPLICATION . $controllerFolder.'/' . str_replace('../', '', $path) . '.php';
				
				$this->class = $classPrefix.'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path);

				array_shift($parts);
				
				break;
			}
		}
		
		if ($args) {
			$this->args = $args;
		}
			
		$method = array_shift($parts);
				
		if ($method) {
			$this->method = $method;
		} else {
			$this->method = 'index';
		}
	}
Basically, i moved the code from the __construct into a new method, called routeToFile() and added some custom code in the __contruct().
With the above implementation, you can add custom controllers in the "custom_controller" folder (located at same level with "controller" folder) and following the same structure as the controller folder.
Here is an example, override/extend the "catalog/controller/common/home.php"

Code: Select all

// file: catalog/custom_controller/common/home.php
<?php  
class CustomControllerCommonHome extends ControllerCommonHome {
	public function index() {
           parent::index();
	}
}
?>
You can directly extend the parent controllers(in this case "ControllerCommonHome") or you can just extend the "Controller" controller, everyway will work.

I haven't had time to test this too much, but so far i haven't seen any problem with this approach, so i hope it helps somebody having this issue.

Newbie

Posts

Joined
Wed Mar 28, 2012 11:04 pm

Post by JNeuhoff » Tue Apr 03, 2012 8:24 pm

Interesting.

At the moment, if there is a need to modify an existing controller you should use VQmod.

Having said that, in the long term OpenCart needs a proper plugin and/or extension mechanism.

The biggest challenge will be the ability to cope with multiple addons, which aren't aware of each other, and which happen to try to modify the same OpenCart core file, e.g. a controller or model or template.

VQmod solves this to an extend, at the expense of having to use an XML script for the modifications. Personally, I think using an XML notation for programming logic is not the ideal way, XML is good for structured data, but not for writing pieces of a program.

Over the past few weeks I have been doing some research, looking into different ways to solve this problem, such as Aspect Oriented Programming, or using Interceptors, obersver patterns, and some other ways, such as dynamic class extensions etc. I came up with 2 solutions which can potentially replace VQmod using plain PHP only, no XML scripting, I am currently testing them for a few addons, and if successful, hope to present these solutions on this forum soon.

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Expert Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by lperry65 » Sun Jun 03, 2012 7:38 pm

There is no way I will ever use VQmod, I just hate it. This mans that I either go without, write my own or use a different solution to OC. Although I'm not a big fan of Magento it's fallback system works well, would it not be possible ti implement a similar solution in OC?

Newbie

Posts

Joined
Fri Dec 02, 2011 4:37 am

Post by straightlight » Sat Jun 09, 2012 5:49 am

lperry65 wrote:There is no way I will ever use VQmod, I just hate it. This mans that I either go without, write my own or use a different solution to OC. Although I'm not a big fan of Magento it's fallback system works well, would it not be possible ti implement a similar solution in OC?
There has been a few discussion on the forum regarding the idea of developing dynamic pages (blocks) over database like Boonex Dolphin already provides. Having this utility over OC would greatly improve its reputation.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by JNeuhoff » Sat Jun 09, 2012 6:29 am

straightlight wrote:
lperry65 wrote:There is no way I will ever use VQmod, I just hate it. This mans that I either go without, write my own or use a different solution to OC. Although I'm not a big fan of Magento it's fallback system works well, would it not be possible ti implement a similar solution in OC?
There has been a few discussion on the forum regarding the idea of developing dynamic pages (blocks) over database like Boonex Dolphin already provides. Having this utility over OC would greatly improve its reputation.
What does Dolphin do to resolve the problem of extending the same core OpenCart classes by different addons?

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Expert Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by straightlight » Sat Jun 09, 2012 11:14 pm

Nothing on that end. It was simply mentioned based on creating dynamic pages (blocks) as stated above which would be more revolutionized idea than having to manually expand the controllers for most contributions.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by JNeuhoff » Fri Sep 28, 2012 8:56 pm

I have created a new system engine for OpenCart which supports the same MVC framework.

It allows the extension of controller, model, and library classes in order to override methods.

Multiple addons, which aren't aware of each other, can extend the same Opencart core class, I have builtin mechanisms for preventing clashes.

Language files and and templates can also be modified by addons.

Hope to upload it all this weekend for public testing. Still have to finish the documentation for it before doing so.

Update: The new Override Engine is now available as a free addon.

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Expert Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by artcore » Tue Jul 09, 2013 4:30 am

Hi all, I'm just starting to dig deep into OpenCart and found myself editing the controllers to get the results I want, mainly header.php and seo_url.php at this point.
I don't want to get into vqMod, I'd rather exhaust alternatives like this one.

I'm interested in trying this method, but when I edit the action.php with the above code I get a syntax error:
'unexpected T_CLASS on line 1'
I replaced the public function __construct so that it looks like below. I counted all the {} and checked ';' - but this is where Google left me. Can anyone shed some light? I'd be very grateful. Maybe it has to do with this post being 1 year old and applies only to an older version?

Code: Select all

<?php

final class Action {

	protected $file;

	protected $class;

	protected $method;

	protected $args = array();
	
	
	
	public function __construct($route, $args = array()) {
      // run default implementation
        $this->routeToFile($route, $args);
       
        //if we found a base controller, we will include it,
        //so that later we may extend it, or just override it completely.
        if(!empty($this->file)&&is_file($this->file)){
            require_once($this->file);   
        }
        //store the current values in a variable so that we can assign them back later if it's the case,
        //if there is no custom implementation of our current controller, and reset each property of the object.
        $props=get_object_vars($this);
        foreach($props AS $key=>$value){
           $this->$key=null;
        }
       
        //look for a custom controller.
        $this->routeToFile($route, $args, 'custom_controller','Custom');
       
       //if there is no file, means we did not find a custom controller,
        //so we assign the base controller values back.
        if(empty($this->file)||!is_file($this->file)){
           foreach($props AS $key=>$value){
              $this->$key=$value;
          }
       }   
    }
   
      protected function routeToFile($route, $args = array(), $controllerFolder='controller', $classPrefix='') {
      $path = '';
      
      $parts = explode('/', str_replace('../', '', (string)$route));
      
      foreach ($parts as $part) {
         $path .= $part;
         
         if (is_dir(DIR_APPLICATION . $controllerFolder. '/' . $path)) {
            $path .= '/';
            
            array_shift($parts);
            
            continue;
         }
         
         if (is_file(DIR_APPLICATION . $controllerFolder. '/' . str_replace('../', '', $path) . '.php')) {
            $this->file = DIR_APPLICATION . $controllerFolder.'/' . str_replace('../', '', $path) . '.php';
            
            $this->class = $classPrefix.'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path);

            array_shift($parts);
            
            break;
         }
      }
      
      if ($args) {
         $this->args = $args;
      }
         
      $method = array_shift($parts);
            
      if ($method) {
         $this->method = $method;
      } else {
         $this->method = 'index';
      }
   }

   public function getFile() {
		return $this->file;
	}

	public function getClass() {
		return $this->class;
	}

	public function getMethod() {
		return $this->method;
	}
	public function getArgs() {
		return $this->args;
	}
}
?>

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by pedro1993 » Tue Jul 09, 2013 4:47 am

Can you please explain what you are trying to achieve here?

Peter
artcore wrote:Hi all, I'm just starting to dig deep into OpenCart and found myself editing the controllers to get the results I want, mainly header.php and seo_url.php at this point.
I don't want to get into vqMod, I'd rather exhaust alternatives like this one.

I'm interested in trying this method, but when I edit the action.php with the above code I get a syntax error:
'unexpected T_CLASS on line 1'
I replaced the public function __construct so that it looks like below. I counted all the {} and checked ';' - but this is where Google left me. Can anyone shed some light? I'd be very grateful. Maybe it has to do with this post being 1 year old and applies only to an older version?

Code: Select all

<?php

final class Action {

	protected $file;

	protected $class;

	protected $method;

	protected $args = array();
	
	
	
	public function __construct($route, $args = array()) {
      // run default implementation
        $this->routeToFile($route, $args);
       
        //if we found a base controller, we will include it,
        //so that later we may extend it, or just override it completely.
        if(!empty($this->file)&&is_file($this->file)){
            require_once($this->file);   
        }
        //store the current values in a variable so that we can assign them back later if it's the case,
        //if there is no custom implementation of our current controller, and reset each property of the object.
        $props=get_object_vars($this);
        foreach($props AS $key=>$value){
           $this->$key=null;
        }
       
        //look for a custom controller.
        $this->routeToFile($route, $args, 'custom_controller','Custom');
       
       //if there is no file, means we did not find a custom controller,
        //so we assign the base controller values back.
        if(empty($this->file)||!is_file($this->file)){
           foreach($props AS $key=>$value){
              $this->$key=$value;
          }
       }   
    }
   
      protected function routeToFile($route, $args = array(), $controllerFolder='controller', $classPrefix='') {
      $path = '';
      
      $parts = explode('/', str_replace('../', '', (string)$route));
      
      foreach ($parts as $part) {
         $path .= $part;
         
         if (is_dir(DIR_APPLICATION . $controllerFolder. '/' . $path)) {
            $path .= '/';
            
            array_shift($parts);
            
            continue;
         }
         
         if (is_file(DIR_APPLICATION . $controllerFolder. '/' . str_replace('../', '', $path) . '.php')) {
            $this->file = DIR_APPLICATION . $controllerFolder.'/' . str_replace('../', '', $path) . '.php';
            
            $this->class = $classPrefix.'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path);

            array_shift($parts);
            
            break;
         }
      }
      
      if ($args) {
         $this->args = $args;
      }
         
      $method = array_shift($parts);
            
      if ($method) {
         $this->method = $method;
      } else {
         $this->method = 'index';
      }
   }

   public function getFile() {
		return $this->file;
	}

	public function getClass() {
		return $this->class;
	}

	public function getMethod() {
		return $this->method;
	}
	public function getArgs() {
		return $this->args;
	}
}
?>

For OpenCart & PHP/MySQL support feel free to PM me :)
Click here for my extentions
Did I help you? Donate here to show support


Active Member

Posts

Joined
Tue Oct 18, 2011 4:31 am
Location - Glasgow, Scotland

Post by JNeuhoff » Tue Jul 09, 2013 5:14 am

artcore wrote:Hi all, I'm just starting to dig deep into OpenCart and found myself editing the controllers to get the results I want, mainly header.php and seo_url.php at this point.
I don't want to get into vqMod, I'd rather exhaust alternatives like this one.

I'm interested in trying this method, but when I edit the action.php with the above code I get a syntax error:
'unexpected T_CLASS on line 1'
I replaced the public function __construct so that it looks like below. I counted all the {} and checked ';' - but this is where Google left me. Can anyone shed some light? I'd be very grateful. Maybe it has to do with this post being 1 year old and applies only to an older version?
Have you tried our Override Engine which allows you to extend controller classes and then override their methods?
See our signature below.

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Expert Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by artcore » Tue Jul 09, 2013 5:31 am

Thanks for the replies guys!

I added several things/variables to catalog/controler/common/header.php like;
-custom page title with a.o. brand, product
-category description + thumb (for display in dropdown menu)
-Extra links for manufacturer,contact.specials etc, also in the menu

The seo_url will contain the 'missing' sef for the non-shop pages like information, account, and so on.

To survive an update and keep track of my customizations I'd like to extend the CommonHeader with my CustomHeader if possible. (Maybe with a getChild function as I've also found on the forum.)
At this point I'm not looking into vqMod or The Override Engine, but my limited knowledge might force me too:)
The solution of this topic seems very elegant so I'm eager to try it.

Cheers

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by pedro1993 » Tue Jul 09, 2013 5:33 am

You are best simply using VQMod, or extending the CommonHeader. It would be the easiest way I think.
artcore wrote:Thanks for the replies guys!

I added several things/variables to catalog/controler/common/header.php like;
-custom page title with a.o. brand, product
-category description + thumb (for display in dropdown menu)
-Extra links for manufacturer,contact.specials etc, also in the menu

The seo_url will contain the 'missing' sef for the non-shop pages like information, account, and so on.

To survive an update and keep track of my customizations I'd like to extend the CommonHeader with my CustomHeader if possible. (Maybe with a getChild function as I've also found on the forum.)
At this point I'm not looking into vqMod or The Override Engine, but my limited knowledge might force me too:)
The solution of this topic seems very elegant so I'm eager to try it.

Cheers

For OpenCart & PHP/MySQL support feel free to PM me :)
Click here for my extentions
Did I help you? Donate here to show support


Active Member

Posts

Joined
Tue Oct 18, 2011 4:31 am
Location - Glasgow, Scotland

Post by artcore » Tue Jul 09, 2013 5:45 am

pedro1993 wrote:...or extending the CommonHeader. It would be the easiest way I think.
That is precisely what I'm trying to achieve. Can you elaborate on how to extend the base controllers?

Cheers

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by pedro1993 » Tue Jul 09, 2013 5:56 am

Sorry, I meant "editing" not "extending". I think editing CommonHeader would be the best way.
artcore wrote:
pedro1993 wrote:...or extending the CommonHeader. It would be the easiest way I think.
That is precisely what I'm trying to achieve. Can you elaborate on how to extend the base controllers?

Cheers

For OpenCart & PHP/MySQL support feel free to PM me :)
Click here for my extentions
Did I help you? Donate here to show support


Active Member

Posts

Joined
Tue Oct 18, 2011 4:31 am
Location - Glasgow, Scotland

Post by artcore » Fri Jul 12, 2013 1:35 am

Got it working:)
It seems

Code: Select all

parent::index();
is not needed in v1.5.5.1

Thanks for the replies!

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands
Who is online

Users browsing this forum: No registered users and 4 guests