We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33976 ☆ A M B ☆
    • 571 Posts
    Hi all,

    There are times when you need some custom routing for your development.
    I'm tired of doing it over and over again for each component, so i'm starting the journey of trying to abstract it in a dedicated component.

    Basically, i'm aware of 2 methods to achieve this :

    1 - doing it the "Discuss way" (some rewrite rules, controllers & a "router" resource)
    2 - using some "placeholder" resource to sendForward() to when OnPageNotFound event is triggered

    I was just wondering what method you guys are using, as well as what are your usual needs in custom routing ? (ie. the kind of constraints to satisfy before allowing the route to be processed).

    For the record, here a route configuration array, hoping that might talk to you guys smiley

    <?php
    return array(
        // The context the routes are allowed in
        'web' => array(
            // The request method
            'GET' => array(
                // The route pattern {ObjectClass:column}, object class being also used as parameter name, column being optional (defaulting to alias)
                '/pro/{Professional:alias}' => array(
                    // Optional service class to load
                    'service' => 'Professionals',
                    // Some constraints to make sure are satisfied before routing
                    'constraints' => array(
                        // Parameter name (object class name) => array of constraints
                        'Professional' => array(
                            'published' => true,
                        ),
                    ),
                    // Optional Route derivated class to instanciate
                    'class' => 'ProRouter',
                    // Optional class path
                    'class_path' => dirname(__FILE__) . '/routes/',
                ),
            ),
        ),
    );
    


    I definitely would love to get your feedbacks & ideas wink
    Thanks
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Great question. I've generally done just the OnPageNotFound method to tie to a plugin or a resource that is devoted to pulling up custom data (e.g. products/* would map to a Snippet that pulls up product records).

      But this brings up a good point: can MODX do more? Can we expose the routing logic beyond the simplistic "OnPageNotFound" catch-all? I think a more standard routing configuration (a la CodeIgniter, Laravel, etc) could open up brave new worlds...
        • 18373 ☆ A M B ☆
        • 3,141 Posts
        I noticed this the other day: http://modx.com/extras/package/customrequest which seems to be pretty cool and abstracted.

        To answer your question, I think the ideal routing would do both. Use rewrites for best performance, but fall back to a plugin triggering on OnPageNotFound. Wether the controller is a resource or a controller class like Discuss would probably depend on the specific extra. Controller classes tend to, in my experience, be a bit less flexible to set up for users unless it's made a whole lot of work, like in Discuss. [ed. note: markh last edited this post 13 years, 3 months ago.]
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 33976 ☆ A M B ☆
          • 571 Posts
          Thanks guy,

          For the record (and to continue the listing of existing solutions), i've been told on Twitter to check SimplxController.

          @Everett : another option could be to extend modRequest or making use of OnHandleRequest system event. But i seriously doubt some routing system à la CodeIgniter/Laravel is doable right now. But as far as i remind, custom routing is on the roadmap.

          @Mark : the drawback i see in using rewrites rules is that it's neither server (httpd) agnostic nor flexible (when adding/removing routes, you would have to edit your rewrite rules).
          But just for the sake of learning/exploration (and eventually build an "i can route Discuss stuff too" component), i'll give it a go wink

          Quote from: markh at Jul 05, 2013, 03:01 PM
          Controller classes tend to, in my experience, be a bit less flexible to set up for users unless it's made a whole lot of work, like in Discuss.

          I'm not sure about that (unless you want to customize the routing logic). End user would just have to set the rewrites rules. On the other side, what i really like in this method is the file based theming!

          To keep going with the "requirements/constraints", i guess we need support for objects relationships (ie. "/route_to/{category}/{object}" where object have a relationship to category) as well as some kind of validation (ie. "/route_to/{year}" where year is a 4 digits strings).