Skip to content
Advertisement

Adding class to Opencart action file

Actually I am trying to add class (Controllerjournal3flashsale) to action.php I want this class because I am using dynamic block from journal 3 theme and it’s not working when i add block under layout it’s showing only that dynamic block content instead of showing rest of the page content together. I tried so many ways but giving error:

Error is:

Fatal error: Uncaught Error: Class 'Controllerjournal3flashsale' not found in /home/mywebsite/storage_aelive/modification/system/engine/action.php:71 Stack trace: #0 /home/mywebsite/storage_aelive/modification/system/engine/loader.php(27): Action->execute(Object(Registry), Array) #1 /home/mywebsite/public_html/catalog/controller/journal3/blocks.php(133): Loader->controller('journal3/flash_...', Array) #2 /home/mywebsite/public_html/live/library/journal3/opencart/modulecontroller.php(149): ControllerJournal3Blocks->beforeRender() #3 /home/mywebsite/public_html/catalog/controller/journal3/blocks.php(18): Journal3OpencartModuleController->index(Array) #4 /home/mywebsite/storage_aelive/modification/system/engine/action.php(79): ControllerJournal3Blocks->index(Array) #5 
/home/mywebsite/storage_aelive/modification/system/engine/loader.php(27): Action->execute(Object(Registry), Array) #6 
/home/mywebsite/public_html/live/library/journal3/opencart/controller.php(112): Loader->controller('journal3/blocks', Array) #7 /home/clearan in /home/mywebsite/storage_aelive/modification/system/engine/action.php on line 71

And the action.php file code is here:

class Action {
    private $id;
    private $route;
    private $method = 'index';

    /**
     * Constructor
     *
     * @param   string  $route
    */
    public function __construct($route) {
        $this->id = $route;

        $parts = explode('/', preg_replace('/[^a-zA-Z0-9_/]/', '', (string)$route));

        // Break apart the route
        while ($parts) {
            $file = DIR_APPLICATION . 'controller/' . implode('/', $parts) . '.php';

            if (is_file($file)) {
                $this->route = implode('/', $parts);        

                break;
            } else {
                $this->method = array_pop($parts);
            }
        }
    }

    /**
     * 
     *
     * @return  string
     *
    */  
    public function getId() {
        return $this->id;
    }

    /**
     * 
     *
     * @param   object  $registry
     * @param   array   $args
    */  
    public function execute($registry, array $args = array()) {
        // Stop any magical methods being called
        if (substr($this->method, 0, 2) == '__') {
            return new Exception('Error: Calls to magic methods are not allowed!');
        }

        $file  = DIR_APPLICATION . 'controller/' . $this->route . '.php';   
        $class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->route);

        // Initialize the class
        if (is_file($file)) {
            include_once(modification($file));

            $controller = new $class($registry);
        } else {
            return new Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
        }

        $reflection = new ReflectionClass($class);

        if ($reflection->hasMethod($this->method) && $reflection->getMethod($this->method)->getNumberOfRequiredParameters() <= count($args)) {
            return call_user_func_array(array($controller, $this->method), $args);
        } else {
            return new Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
        }
    }
}

Advertisement

Answer

I suggest you to use a controller structure from journal manual https://docs.journal-theme.com/docs/modules/blocks#dynamic-blocks.

Theme comes with an example, you can find it in catalog/controller/journal3/blocks_example.php

use Journal3OpencartController;

class ControllerJournal3BlocksExample extends Controller {

    public function index($args) {
        return 'Dynamic Content for module_id = ' . $args['module_id'];
    }

}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement