Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    writing an effective router/dispatcher for mvc pattern ??

    hello forums !!
    I am trying to make an router/dispatcher for oo mvc pattern.
    I would like to have one default router pattern ie controller/method/id and other is customized one
    for example
    PHP Code:
    http://yoursite.com/controller/method/id/params => $controller->method($id[, $params])(general)
    http://yoursite.com/admin/controller/method/id/params => $controller->admin_method($id[, $params]) (customized) 
    Can anybody provide good suggestions/samples in dispatching such method.
    Note: dispatching method should work both for url rewriting turned on or off by changing some flags.

    Thanks in advance for your valueable suggestions.

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Knock Knock... Can anybody come with better ideas

  • #3
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I was creating a blogging system and created something like this. You will have to customise it but here it is:

    PHP Code:
    <?php

        
    echo '<p>This is our index.php file!</p>';
        
        
    // Requested URL
        
    $url $_SERVER['REQUEST_URI'];
        
        
    // Removes App Root from URL
        
    $url str_replace('/BlogSite/'''$url);
        
        
        
    $routes = array(
        
                        array(
    'url' => '/^posts\/(?P<id>\d+)$/''controller' => 'posts''view' => 'show'),
                        array(
    'url' => '/^posts\/(?P<id>\d+)\/edit$/''controller' => 'posts''view' => 'edit')
                        
                        );
        
            
    $params = array();
            
            
    $route_match false;
        
        foreach(
    $routes as $urls => $route) {
        
            if(
    preg_match($route['url'], $url$matches)) {
                
                
    $params array_merge($params$matches);
                    
                  
    $route_match true;
                    
                    break;
            
            }
        }
        
        if(!
    $route_match) { exit('No Route Found!'); }
        
        include(
    'controller/'.$route['controller'].'.php');

    ?>
    I used a .htaccess file so all my /dirs/ get passed through the index.

    Hope this helps.

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Any Good?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •