Jump to content

How to use Hook addWebserviceResources?


Recommended Posts

Hello,

 

is there a code example available of the hook addWebserviceResources? I want to add a custom webservice resource, but all tutorials are outdated and are based on class override (which isn't possible in PrestaShop 1.7).

 

Best regards

Link to comment
Share on other sites

Trying this ends in an semi white page:

 

    public function hookAddWebserviceResources($extra_resources)
    {
        $extra_resources['resources']['mypageresource'] = array('description' => 'Manage My Pages', 'class' => 'CMS');
        return $extra_resources;
    }
Link to comment
Share on other sites

Hi

 

Not sure how that hook works, but check few things first. Try to enable debug mode and see why you get white page.

 

Also CMS is already available in web service 

 'content_management_system' => array('description' => 'Content management system', 'class' => 'CMS'),

classes/webservice/WebserviceRequest.php line 302.

Link to comment
Share on other sites

After inserting some "debug" output in WebserviceRequest.php it seems this is the correct syntax:

    public function hookAddWebserviceResources()
    {
        return array('mypageresource' => array('description' => 'Manage My Pages', 'class' => 'CMS'));
    }
  • Like 1
Link to comment
Share on other sites

Of course it was just a test. Finally I do something like this:
 

require_once(_PS_MODULE_DIR_.'mymodule/classes/customClass.php');
require_once(_PS_MODULE_DIR_.'mymodule/classes/extraClass.php');
 
class MyModule extends Module
{
    public function __construct()
    {
        // Basic module stuff here...
    }

    public function install()
    {
        return parent::install() && $this->registerHook('addWebserviceResources');
    }

    public function hookAddWebserviceResources()
    {
        return array(
            'custom' => array(
                'description' => 'My Custom Resource',
                'class' => 'custom'
            ),
            'extra' => array(
                'description' => 'My Extra Resource',
                'class' => 'extra'
            )
        );
    }
}

mymodule/classes/customClass.php looks like:
 

class custom extends CMS {
    // extend or adjust CMS stuff here
}
Edited by frojha (see edit history)
  • Like 2
  • Thanks 2
Link to comment
Share on other sites

  • 2 years later...
  • 5 months later...
  • 1 month later...
  • 3 months later...
  • 6 months later...
  • 2 months later...

ObjectModel works only with tables from database, for example:

namespace PrestaShop\Module\ApiFish\Controller\Api;

use ObjectModel;

class Custom extends ObjectModel {
    /**
     * @see ObjectModel::$definition
     */
    public static $definition = [
        'table' => 'attribute',
        'primary' => 'id_attribute',
        'multilang' => true,
        'fields' => [
            'id_attribute_group' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
            'color' => ['type' => self::TYPE_STRING, 'validate' => 'isColor'],
            'position' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],

            /* Lang fields */
            'name' => ['type' => self::TYPE_STRING, 'lang' => true, 
			'validate' => 'isGenericName', 'required' => true, 'size' => 128],
        ],
    ];
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...