We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17931
    • 24 Posts
    Hi all,

    Maybe this question is naïve. I am trying to write a module. The proper module just includes a main php script. In that main file, I want to invoke another php script.

    My problem is that this script knows nothing about the variables which are available in the module. How can that be done? I would prefer not to use a submit button.

    module:
    $basePath = $modx->config['base_path'];
    //$modPath comes from the module configuration
    include($basePath . $modPath . "main.php");
    


    main.php
    <http>
    <head>
    ...
    </head>
    <body>
    ...
    I want, for example, this link to execute: <a href="script.php" >do something</a>
    ...
    </body>
    </html>
    


    script.php:
    <?php
    //make an sql query
    //render the results in a table
    include $basePath . $modPath . "maketable.class.php";
    //etc
    ?>
    


    The logs say:
    ... Undefined variable: modPath ...
    ... Undefined variable: basePath ...

    Thanks!
    João

      • 10449
      • 956 Posts
      You could include() this library: http://www.modxcms.com/MODxAPI-Library-865.html


      This Library can be very useful to those who would like to connect to and execute MODx documents and APIs from an external application.

      // path to modxapi library
      $path = dirname(__FILE__).’/’;

      include_once($path.’modxapi.php’);

      $modx = new MODxAPI();
        • 17931
        • 24 Posts
        Hi ganesh,

        Thanks for your reply.
        I would have to do

        $path = dirname(__FILE__).'/';
        include_once($path.'modxapi.php');
        $modx = new MODxAPI();
        


        in every script, right? That would do it in case I only needed $modx. More importantly, the problem is that I want to have my own variables to be passed on to script.php, other than $modx, so other things have yet to be done.

        I realize this is more a problem of PHP architecture than MODx. But still the answer eludes me! Maybe if the module code shares a header with the remaining scripts, by means of include_once()?

        Thanks,
        João


          • 10449
          • 956 Posts
          Feel free to poke around the existing module code, to find out how they did it. It’s a great way of learning.
          The 2 or 3 modules that are pre-installed write basically all their code into the main module-window and hence share all those global vars automatically.
          Most "actions" are then somehow separated by switch() statements.

          But if you just insert an iframe with some standalone php script that does its own thing, this "outside" file has no connection to the rest of modx. It’s just two separate files your browser is fetching, and that’s it.

          If you work with independent files on your filesystem and you don’t include(), but only link to them, you have to "connect" to the global $modx vars; that’s where the API fits in.