We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8579
    • 4 Posts
    Hi, I have a query that's more semantics/good practise than actually functionality.

    I have created a CMP page in the administration for the control of some entries, but I was wondering what the correct way to perform routing between different manager pages is.
    This is the code I'm currently using - it works, but I'm sure there's a better way. Overriding a function that should return a 'default' controller seems like a bad move, but I couldn't find another function that would be better suited.
    class IndexManagerController extends modExtraManagerController
    {
      public static function getDefaultController()
      {
        switch( $_REQUEST['p'] )
        {
          case 'users':
            $return = 'users'; break;
          case 'entries': default:
            $return = 'home';
        }
          
        return $return;
      }
    }

    Any pointers or advice would be greatly appreciated; many thanks!
      Ellis Grouse / Development Ninja

      <a href="http://egrouse.com" target="_blank">egrouse.com</a> | <a href="mailto:[email protected]">[email protected]</a> | <a href="http://twitter.com/_egrouse" target="_blank">Twitter</a> | <a href="https://plus.google.com/111889613851379377602/about" target="_blank">Google+</a> | <a href="http://www.linkedin.com/in/egrouse" target="_blank">LinkedIn</a>
      • 28215
      • 4,149 Posts
      Look into how Quip does it, especially looking at the code for modExtraManagerController - You shouldn't need the switch statement; it checks automatically for the $_REQUEST['action'] var.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 8579
        • 4 Posts
        Thanks for the recommendation, have just had a look.

        From what I can see there is only one actual manager page (with menu access), though. I had previously implemented modExtraManagerController exactly as it is in Quip - all the code I have so far is based off the extra tutorial, which I know realise you wrote.

        What I'm basically trying to do is have two distinct pages in the administration area with different functions, so at the moment I'm switching based upon an extra request variable - both the pages have the same action, as this was the only way I could determine to get it to use the same modExtraManagerController (index.class.php) for both of the pages. The page class files (home.class.php) are then loaded via the switch.

        Am I expected to use a separate modExtraManagerController and therefore different 'index' class file for each page? I figured that the idea of such a file was to include things common to each of the lesser (home, etc) class files. Is this incorrect, or am I missing something simple (as usual).

        Cheers!
          Ellis Grouse / Development Ninja

          <a href="http://egrouse.com" target="_blank">egrouse.com</a> | <a href="mailto:[email protected]">[email protected]</a> | <a href="http://twitter.com/_egrouse" target="_blank">Twitter</a> | <a href="https://plus.google.com/111889613851379377602/about" target="_blank">Google+</a> | <a href="http://www.linkedin.com/in/egrouse" target="_blank">LinkedIn</a>
          • 28215
          • 4,149 Posts
          No, only one is needed. See:

          https://github.com/splittingred/Quip/blob/develop/core/components/quip/index.class.php

          Note how IndexManagerController extends QuipManagerController, which extends modExtraManagerController. That last class is important - modExtraManagerController - because it automatically routes you according to the $_GET['action'] variable. The QuipManagerController just provides all of our Quip-specific controllers with a few extra methods, and access to the $quip instance variable. That's all it does - and note it's abstract so it cannot be instantiated directly.

          Now, in thread.class.php, found here:
          https://github.com/splittingred/Quip/blob/develop/core/components/quip/controllers/thread.class.php

          Note how it extends QuipManagerController - this means that the routing will automatically be handled, and this class will be loaded if $_GET['action'] is equal to "thread". So if I pass /modx/manager/?a=123&action=thread (where 123 is my modAction ID for the CMP), it will load that controller.

          Similarly, if I passed 'home', it would load home.class.php, found here:
          https://github.com/splittingred/Quip/blob/develop/core/components/quip/controllers/home.class.php

          Furthermore, IndexManagerController is an interesting class, because you're not really ever loading it - you're just saying, hey! Don't load me! Load the 'home' action instead:

          public static function getDefaultController() { return 'home'; }
          


          This method in IndexManagerController tells MODX to not load IndexManagerController, but rather HomeManagerController (iow, it simulates $_GET['action'] being set to 'home' if $_GET['action'] isn't set).

          I've updated the RTFM tutorial, and the Doodles repository to better reflect this.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 8579
            • 4 Posts
            Aha, I see! Thanks for your help - have implemented the code now as shown and is working as expected. Onwards with the further development!

            Cheers smiley
              Ellis Grouse / Development Ninja

              <a href="http://egrouse.com" target="_blank">egrouse.com</a> | <a href="mailto:[email protected]">[email protected]</a> | <a href="http://twitter.com/_egrouse" target="_blank">Twitter</a> | <a href="https://plus.google.com/111889613851379377602/about" target="_blank">Google+</a> | <a href="http://www.linkedin.com/in/egrouse" target="_blank">LinkedIn</a>
              • 39742
              • 29 Posts
              Sorry for barking in but I have a doubt that relates to this topic...not that important but nevertheless here it goes...
              It's about the default path to search processors.
              I understand the index class (modExtraManagerController) reads action (createdoodle for example) parameter and routes the request to the "components/doodles/controllers/createdoodle.class.php"
              If I also like to add an "action" through modx manager interface (and eventually assign it to a menu) then it would search the "components/doodles" folder and I would have to create another createdoodle.class.php file that redirects to the former like "index" redirects to "home"
              Is this the way it is supposed to be done if I want to add a "create doodle" menu action?
              Thanks