We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3496
    • 101 Posts
    I’m trying to create MODx documents from my own PHP code, but I’m not very successful.
    The pages are created, but viewing doesn’t work (500 error) - and the context doesn’t seem to really be set (at least retrieving modContext is not successful).
    At first I though that it probably was an authorisation problem, but after loging in with a manager-account at the front end the results are the same.

    I’m obviously doing something wrong, but what?
      • 28215
      • 4,149 Posts
      First off, it should be:

      $contextObj=$resource->getOne('Context');
      

      instead of ’modContext’.

      Secondly, you don’t have to grab the Context object; could just do:

      $output = $resource->get('context_key');
      


      Thirdly, is this in a Snippet? if so, you should not be ’echo’ing - return the output into a variable. Also, why are you running $resourceContext->prepare()? You don’t need to.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 3496
        • 101 Posts
        Thanks for the corrections; now I can indeed get the resource object - and you’re completely right: get(’context_key’) would have been better.
        The prepare() was copied from the create resource processor in the MODx core (the example I worked from). Good to here that it’s not useful; I will remove it from my code.

        About the use of echo statements:
        this was just a simple test-case - and the real code is part of a large class, that returns the results to the main-program (no echo-statements in that part of the code).
        But I must admit that for my main PHP code I still need to make the switch to a better MODx/Snippet/placeholder -based way of working (yes currently it still contains lots of echo-statements - and I also do not know yet what would be the right solution for debugging and error-reporting - so much to learn...).
        You also asked whether it is a snippet; Currently my code is just included in a snippet - and is stored as a seperate PHP file.

        But for the main problem:
        the code now reports that everything goes well and the page does indeed show up in the menu (as it did earlier) and it is shown in the manager in the ’web’ context (also as it did earlier) - but when I try to open the page I get "500 Error" "A fatal application error has been encountered".
        Do you have any idea about what could cause this?

        See attachment for the now somewhat shorter version of the code.
          • 22303 MODX Staff
          • 10,725 Posts
          Anything in your error log? See Reports --> Error Log
            • 3496
            • 101 Posts
            Hmmm, I should have looked at "reports" earlier - I was even searching for a way to get to the MODx logging and didn’t even think of looking in the manager; I think I need to catch up on sleep or something smiley

            Thanks!

            The error is:
            [2010-02-18 22:44:05] (FATAL) The requested resource has no valid content type specified.

            I used the statement:
            $resource->set(’content_type’, ’text/html’);

            And indeed: if I leave that line out the problem is solved!

            (by the way: what would be a valid content type?)
              • 3749
              • 24,544 Posts
              Quote from: Black at Feb 18, 2010, 03:52 PM

              (by the way: what would be a valid content type?)

              $resource->set('content_type',1)


              (but it’s the default anyway). smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 3496
                • 101 Posts
                Thanks Bob!
                A very short name, but that has the advantage of very little chance of typing errors
                smiley
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: Black at Feb 19, 2010, 02:24 AM

                  Thanks Bob!
                  A very short name, but that has the advantage of very little chance of typing errors
                  smiley
                  That is not the name of a content type, it is the primary key representing the row in the content_type table (represented by the modContentType class).
                    • 3749
                    • 24,544 Posts
                    Quote from: OpenGeek at Feb 19, 2010, 08:54 AM

                    Quote from: Black at Feb 19, 2010, 02:24 AM

                    Thanks Bob!
                    A very short name, but that has the advantage of very little chance of typing errors
                    smiley
                    That is not the name of a content type, it is the primary key representing the row in the content_type table (represented by the modContentType class).

                    It might be worth defining some constants for that.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 28215
                      • 4,149 Posts
                      Quote from: BobRay at Feb 19, 2010, 04:52 PM

                      It might be worth defining some constants for that.

                      Nope; since they’re dynamic, based on whatever ones you create in System -> Content Types. Try:

                      $contentType = $modx->getObject('modContentType',array('mime_type' => 'text/html'));
                      if ($contentType) {
                          $resource->set('content_type',$contentType->get('id'));
                      }
                      
                        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com