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

    i am writing a snippet and need to get the URL of the current document.
    When i try $_SERVER['PHP_SELF'] i allways get "index.php" - no matter what page i am on.

    Here is the code:
    echo"<form id=\"kontaktformular1\" method=\"post\" action=\""; echo $_SERVER['PHP_SELF']; echo"\">";


    Yes, i know you shall not use echo in snippets (even though i don't understand why) - but still i'd like to figure out how it goes.

    Sidequestion: What could i do instead using echo in that snippet? Should i call a chunk from within the snippet?
    And if this would be a way - how would i do this again? Like this?
    <?php [[$chunk]] ?>


    But to come back to my question: I'd be glad if somecone could tell me how to output the URL of the curent page within a snippet.

    Thanky you, Phil
      • 28215
      • 4,149 Posts
      1. Never use echo in snippets (which you know), because that subverts the output and rendering order of the snippet. The proper method is to return.

      2. To get the URL:

      $url = $modx->makeUrl($modx->resource->get('id'));
      
      // or the absolute, full URL
      $url = $modx->makeUrl($modx->resource->get('id'),'','','full');
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 36631
        • 10 Posts
        Hi Splittingred,

        thank you for your answer. It works perfectly well!

        I'll try to keep the echo-thing in mind, eventhough i am not so good with functions and returning - i guess i'll have to put some hours into learning these things.

        Thanks, Phil