Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Controlling Custom Routes (Suggestion)

edited January 2014 in Framework

Let's say I want to use the default 'user' app but want to change urls from mysite.com/user/login to mysite.com/login etc. As I understand it, I'd need to copy the default handler in admin/handlers/page.php to my own handler, and then change the default_handler in conf/config.php, then modify the handler to suit my needs. In the end, all I really want is a bit more control over the routing.

I'd love it if, in the core, we could set custom routes in (a proposed) conf/routes.php, mapping requests to handlers. It would be searched for a match before loading the default_handler.

I see some benefits to this approach. It would:

  1. provide an easy way to control/override urls throughout the entire application.

  2. be faster than checking multiple locations for the existence of a file before returning a handler, if someone wanted to manually define all their routes.

  3. allow us to continue to use the default handler, getting any improvements pushed out in updates without having to copy/merge things over to a custom file.

Comments

  • There are a couple approaches you can take. Within Elefant, you can embed dynamic elements like a user login form into any page using the "dynamic objects" button in the wysiwyg editor (it's the last button to the right in the editor toolbar).

    So the steps would just be to create a page with ID "login" and select "User: Login" from the dynamic objects list. Of course, this only works for handlers that are included in the dynamic objects list.

    You can also implement a custom router by creating a bootstrap.php file in the root of your site. Obviously this example is a bit simplistic, but it does the same as the above:

    <?php // bootstrap.php
    
    if ($_SERVER['REQUEST_URI'] === '/login') {
        $_SERVER['REQUEST_URI'] = '/user/login';
    }
    
    ?>
    

    That will be run just before Elefant does its routing, so you can essentially build any kind of custom routing from the bootstrap file.

    Some of the more complex Elefant apps do exactly this, for example:

    https://github.com/cewca/lemur/blob/master/sample_bootstrap.php https://github.com/cewca/lemur/blob/master/lib/Lemur.php#L10-L22

    Hope that helps!

  • edited January 2014

    Thanks, Johnny. I worked this out in bootstrap.php, anything I didn't think about?

    # Custom routes (request mapping to handler)
    $routes = array(
    
        'login'     =>'user/login',
        'logout'    =>'user/logout',
        'register'  =>'user/signup',
    
    );
    
    foreach ($routes as $route => $handler)
    {
        // points custom routes to handler
        if ($_SERVER['REQUEST_URI'] === '/'.$route)
        {
            $_SERVER['REQUEST_URI'] = $handler;
            break;
        }
        elseif ($_SERVER['REQUEST_URI'] === '/'.$handler)
        {
            // redirects (reverses) handler back to custom route
            header('HTTP/1.1 301 Moved Permanently'); 
            header('Location: /'.$route);
            exit();
        }
    }
    

    *edit - yeah, forgot about the hardcoded form actions.

  • Looks like that should work well as far as I can see.

Sign In or Register to comment.