• How to call a custom PHP file from within ModX#

  • pwaring Reply #1, 2 years, 6 months ago

    Reply
    Hi,
    I'm kind of a ModX 'newbie' so forgive me if this is may seem trivial.

    I am developing an application and I want to seperate out the backend PHP logic coding and the front end template/javascript design. So I decided to use ModX so I could get 1 person on the template HTML design and Javascript and the other person doing the PHP classes for the backend data collection and processing.

    So for example, say I have a resource file or a template in ModX and within that I want to call a Php file that resides somewhere on my server and returns something (let's just say "hello world" to be really original) and display it within a page.

    So let's say the php file resides in www.mydomainname.com/code/helloWorld.php

    Now within ModX let's say I have a normal HTML template:



    So what's the best way to do this?

    Thanks

    Peter


  • sottwell Reply #2, 2 years, 6 months ago

    Reply
    Create a snippet (in the Manager, go to Elements -> Manage Elements, then in the Snippets tab click the New Snippet link)
    <?php
    include "code/helloWorld.php";
    return;
    ?>
    

    Or, if your helloWorld.php is really trivial, just put the code directly in the snippet
    <?php
    return "Hello, World!";
    ?>


    Then in your HTML call the snippet like this:
    [!HelloWorld!]


    http://www.sottwell.com/how-snippets-work.html


  • pwaring Reply #3, 2 years, 6 months ago

    Reply
    Oh my gosh it is THAT easy!!!

    Wow..... I am impressed! :-)

    Thanks for your help!!


  • BobRay Reply #4, 2 years, 6 months ago

    Reply
    There are some circumstances where you might need to use:

    <?php
    return include "code/helloWorld.php";
    ?>
    

    But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.

    One caveat is that the code can't contain interspersed HTML. If there is HTML output to be returned, it should be assigned to a variable and returned from the snippet. Here's an example.

    Wrong:
    <?php
    $count = 6;
    ?>
    <p>Count = <?php echo $count; ?></p>
    <p>end of data</p>


    Right:
    <?php
    $output = '';
    $count = 6;
    $output .= '<p>Count = ' . $count . '</p>';
    $output .= '<p>end of data</p>';
    return $output;
    ?>


  • goldsky Reply #5, 2 years, 6 months ago

    Reply
    Quote from: BobRay at Nov 10, 2009, 11:41 PM
    But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.

    Uhm... isn't this against the upgrade method flexibility?
    If we store the code as the file system, then we only just over write the files.
    As we speak as a developer (although I'm speaking as a newbie), we don't see that as a problem. But after we hand over the system to the client, we should smartly avoid the 'forever call' of web maintenance, shouldn't we?
    Even different views and discussions have been posted several times. Sorry, I'm not trying to hijack the thread, but the issue's always been arise from the first timer snippet author.


  • BobRay Reply #6, 2 years, 6 months ago

    Reply
    Quote from: goldsky at Nov 11, 2009, 12:49 AM
    Quote from: BobRay at Nov 10, 2009, 11:41 PM
    But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.

    Uhm... isn't this against the upgrade method flexibility?
    If we store the code as the file system, then we only just over write the files.
    As we speak as a developer (although I'm speaking as a newbie), we don't see that as a problem. But after we hand over the system to the client, we should smartly avoid the 'forever call' of web maintenance, shouldn't we?
    Even different views and discussions have been posted several times. Sorry, I'm not trying to hijack the thread, but the issue's always been arise from the first timer snippet author.

    It's a good question (if I'm understanding you). As long as the snippet has a unique name (i.e. not called Ditto or Wayfinder, etc.), it will be preserved on upgrade either way. Some developers prefer to leave the snippet in a file for version control and/or using a favorite editor to work on it. OTOH, if it's in a file, it's more of a pain to modify the snippet code in the Manager.

    IIRC, OpenGeek likes to keep the code in files in the root directory and use one custom snippet to "include" any of the snippet files (http://modxcms.com/forums/index.php?topic=18872.40).

    You can certainly do it however you like.