Login to Your Account


Login using your Facebook account

Join the Mercury Community

Click on all the boats to prove you're human.


How To Home | more...

How to make custom module 'router' files

Sometimes you will want to use a URL structure that doesn't conform to the /modulename/filename format.  An example of this might be a blog where you'd like to use something like /blog/categoryname/posttitle-POSTID.html as a path.  This might be the only type of path in your module that doesn't conform to the original standard.  You know that all paths that come to your module will contain two elements but all paths that don't contain exactly two elements (where the first part of the path is not admin) are blogs posts and should be handled by a file called post-detail.php.

Enter router.php

If a module has a file called router.php then the decision about how to parse the incoming path will be processed by this file.  Here is a sample for the above example:

    <?php
        $action = "no-action.php";


        if(count($path) == 2){ // this part catches everything that follows /module/controller syntax
            $action = $path[1] . ".php";

        } else if ($path[0] == "admin") {  // this part allows for the admin files
            $action = $path[2] . ".php";

        }else if(count($path) == 3){ // this handles everything that follows the /blog/category/post-POSTID.html syntax
            $action = "post-detail.php";
            $fileparts = explode(".", $path[2]);
            $parts = explode("-", $fileparts[count($fileparts)-2]);
            $m['BlogPostId'] = end($parts);
        }
   
        require($action);
       
    ?>

In this example the first part catches everything with standard syntax.  The second part catches everything that start with /admin.  The third part break the last element of the path into an array, splitting it on the period then splits it again based on a hyphen.  The very last element of this second array is the post id.  We set the incoming $m variable BlogPostId to be this value and then Mercury includes the post-detail.php file.  The file will then be able to execute as if the BlogPostId variable had been passed in the URL.

Important Note

This is just an example.  You can come up with any URL structure that you want (after the first 'folder' is the name of a module) and write any code you want to handle it.