Search code examples
prestashopprestashop-1.6prestashop-1.7

Prestashop 1.6/1.7: require "autoload.php" in a PHP script in the "web" public folder results in an error 500 (script without namespace)


In mydockercontainer:<mysite>/web/, which is the public directory of my NGINX server Docker's container, I am writting a PHP script which consists in a simple class, out of Prestashop 1.6/1.7 (site is Prestashop 1.7 but I would want to write code in 1.6 for project reasons).

This "simple class" is: mydockercontainer:<mysite>/web/gateway/gateway.php. As you can see it's not a module, nor an overriding of a controller, or something else related to Prestashop. it's just a vanilla PHP script.

However, this script, gateway.php, needs to use some Prestashop's objects. For example Order.

When I try to instanciate Order, I have this error:

Fatal error: Uncaught Error: Class 'Order' not found in /var/www/html/gateway/requestHandler.php:47 Stack trace: #0 /var/www/html/gateway/requestHandler.php(17): RequestHandler->handleOrderUpdate() #1 /var/www/html/gateway/wakeup.php(5): RequestHandler->handleRequest() #2 {main} thrown in /var/www/html/gateway/gateway.php on line 47

So at the beginning of this script, I added the following line: require_once '../autoload.php';. it raises a new error: ERROR 500 but I can't figure out the logs.

Please note that my PHP script doesn't have any namespace since it's in a way out of Prestashop.

What could I do to be able to use an object Prestashop 1.6/1.7 (preference: 1.6) class such as Object in a simple PHP script which is not a Prestashop controller/module/etc.?

Code Snippet

The following code is not entire at all but is sufficient for the question.

File: mydockercontainer:<mysite>/web/gateway/gateway.php (web being the public directory of my Prestashop 1.7 site):

<?php
require_once '../autoload.php';

class Gateway {
    private $request;

    private function handleOrderUpdate() {
        $ps_order = new Order($this->request['id']);    
    }   

}

Solution

  • In order to initiate Prestashop core and use PS objects in a external script you'll need to include

    ../config/config.inc.php
    

    Including autoload.php will not work.