Marek
Marek

Reputation: 1533

Prestashop, cannot require Module class

Problem

Related issue, I have been trying to apply related solution to my project, however I am not able to execute

Module::getInstanceByName('modulename');

I do not even have access to Module class, which makes it hard for me to access application context and my own module context

class_exists('Module')

Returns false.

I also provide the directory structure of my module from which I am attempting to access Module.

├── mymodule.php
├── config.xml
└── somedirectory
    └── index.php

Where mymodule.php follows the module class guidelines and index.php is just php file in which I attempt to access other modules. most important for me is to access the context of my own module so that I can retrieve for example it's version and other settings.

Example source

Example of index.php from my testmodule, this example is for 1.4 but I am also willing to have it working for 1.5 and 1.6

require('../../../config/settings.inc.php');
require('../../../classes/Module.php');
$instance = Module::getInstanceByName('mymodule');

This crashes, and when I use

require('../../../config/settings.inc.php');
require('../../../classes/Module.php');
if (class_exists('Module')) {
    echo "class exists";
} else {
    echo "class does not exists";
}

It outputs class does not exists.

The reason I require ../../../config/settings.inc.php is to demonstrate that there are some classes I can require and it works. I am able to access constants defined inside of settings.inc.php

So the file at relative path ../../../classes/Module.php exists and it contains

...
public static function getInstanceByName($moduleName)
...

Questions

  1. How to properly access Module class to access instance of particular module just as suggested here. What is the proper way of loading Prestashop classes from within an arbitrary file in the custom module.
  2. Why I can import ../../../config/settings.inc.php using require and it doesn't apply to ../../../classes/Module.php?
  3. This issue concerns Prestashop 1.4, however I would like to know if can this be applied also for 1.5 and 1.6, I noticed that for those versions classes/module.php does not exists instead there is classes/module/Module.php. General solution would be the best.

Upvotes: 3

Views: 1217

Answers (1)

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

If you call your index.php file directly (http://www.example.com/modules/mymodule/somedirectory/index.php) you're not using Prestashop internal Dispatcher. This means that Prestashop is not loaded when your index.php script is executed.

If you want to load Prestashop your first need to include /config/config.inc.php:

require('../../../config/config.inc.php');
if (class_exists('Module')) {
    echo "class exists";
} else {
    echo "class does not exists";
}

If you want to use Prestashop standard Dispatcher process, you will have to create a ModuleFrontController.

Upvotes: 2

Related Questions