We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37054
    • 93 Posts
    So in an addon I'm working on at the moment I have stumbled across an issue with the connector.php files which affects all addons with CMP's as far as I can see.

    Basically in each connector we first have a call to require the config.inc.php file at the root of our MODX install to give us the core path from the defined constant MODX_CORE_PATH, fair enough.

    The problem as I see it is that we then go on to require the controller index php file which itself calls in the config.inc.php file in the connectors directory (with the same defined constants as those in the config.inc.php file in our MODX install root) and this means that we are attempting to define the same constants again, namely MODX_CORE_PATH and MODX_CONFIG_KEY. This raises an error in PHP as you can not redefine constants. The fact that we called in our original config.inc.php file with a require_once doesn't help because the second call to config.inc.php in the controller index file is calling the config.inc.php file in the controllers directory - i.e. a different file to the one in our install rooot called in from our connector.php file.

    A simple solution to this that wouldn't break existing addons would be to update the config.inc.php file in the controllers directory so that it changes from:

    define('MODX_CORE_PATH', '/home/mppiintc/public_html/rrc/core/');
    define('MODX_CONFIG_KEY', 'config');
    


    to:
    if(!defined('MODX_CORE_PATH')){
        define('MODX_CORE_PATH', '/home/mppiintc/public_html/rrc/core/');
    }
    if(!defined('MODX_CONFIG_KEY')){
        define('MODX_CONFIG_KEY', 'config');
    }
    


    Is there something I'm missing here? [ed. note: freelancewebdev last edited this post 13 years, 10 months ago.]
      • 22303 MODX Staff
      • 10,725 Posts
      The only file you need to load is config.core.php. The config.inc.php file should NEVER be manually included. Any documentation that says otherwise needs to be changed to reflect this.
        • 37054
        • 93 Posts
        Quote from: opengeek at Nov 11, 2012, 07:47 AM
        The only file you need to load is config.core.php. The config.inc.php file should NEVER be manually included. Any documentation that says otherwise needs to be changed to reflect this.

        Yes it does as every addon I looked at had something like this in its connector.php

        require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
        require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
        require_once MODX_CONNECTORS_PATH.'index.php';
        


        when it should simply be

        require_once dirname(dirname(dirname(dirname(__FILE__)))).'/config.core.php';
        require_once MODX_CONNECTORS_PATH.'index.php';
        


        But that doesn't solve the issue as described of the duplicate definitions for MODX_CORE_PATH and MODX_CONFIG_KEY. [ed. note: freelancewebdev last edited this post 13 years, 10 months ago.]
          • 22303 MODX Staff
          • 10,725 Posts
          I think this is an architectural issue with connectors in general, and why I generally prefer to create my custom admin interfaces and AJAX controllers as Resources outside mgr. It really needs some discussion, because you should be able to use any connector simply by having it include the index.php in your connectors location; nothing else. However, to define connectors in other locations outside of your connectors location would require absolute paths to the main connectors location for the proper include; this leads back to the circular inclusion problem with the config files.

          I'd love to see some deeper discussion on this.
            • 37054
            • 93 Posts
            Well maybe one solution to this is to make the connector itself an object. While I'm only dipping my toes in here atm, I see that there is a connector request class and a connector response class but no connector class. If there was such a class, a custom connector could simply inherit from it much like the way custom processors are handled now and wouldn't need to know where that code was in the file system.
              • 22303 MODX Staff
              • 10,725 Posts
              Unfortunately, that's not going to solve the entire problem. The real issue is connectors become an additional request gateway into MODX, and as the gateway, it is responsible for knowing where a) the MODX core is located and b) in the case of component connectors (typically stored in assets/components/<componentname>/connectors/ or similar), where the "standard" connector index.php is located. a) is easily handled by having a config.core.php with that information, but b) is more problematic. Since the connectors directory is configurable, you cannot know where the index.php include is located until you have loaded the main MODX config file (this is why you see it manually loaded). Complicating the issue further, the main MODX config file is not supposed to be included manually; it is loaded automatically when you create an instance of the modX class.
                • 37054
                • 93 Posts
                Ah OK, looking at the connectors as gateways clarifies the problem you are describing a lot for me. So does the question then become, should connectors be directly exposed in this fashion, should they have the gateway responsibilities you mention? Seems kind of at odds with MVC architecture to me.