SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: MVC routing

  1. #1
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    MVC routing

    Hi i want to create mvc routing, i have seen some routing concept . can any one say how to implemnt

    for example :1) http://sitename.com/controllername?paramname -invoke index method

    2) http://sitename.com/controllername/method/paramname - invoke same controller having anohter method with the parameters pass tothe method.


    we can use ? for getting parameter values else /(Slash) for the parameter values. i m confused with it , for seo we can use / ,if i use /(slash) how can i identify which is method and which is parameter. if the user call method in controller instead of the default method(index). how could i identify the parameter value.



  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    The routing components of today's frameworks can get fairly sophisticated, but if we boil it down to its simplest, identifying a route can be as simple as a set of if-statements. Here's an exmaple.

    PHP Code:
    <?php

    function routingMatch($url)
    {
        
    // root url
        
    if ('/' == $url) {
            return array(
                
    'controller' => 'Default',
                
    'action' => 'index'
            
    );
        }

        
    // blog urls
        //   /blog
        //   /blog/show/{id}
        
    if ('/blog' == $url) {
            return array(
                
    'controller' => 'Blog',
                
    'action' => 'index'
            
    );
        } elseif (
    preg_match('#^/blog/show/(\d+)$#'$url$matches)) {
            return array(
                
    'controller' => 'Blog',
                
    'action' => 'show',
                
    'id' => $matches[1]
            );
        }

        
    // generic urls
        //   /{controllername}
        //   /{controllername}/{method}/{paramname}
        
    if (preg_match('#^/([^/]+)$#'$url$matches)) {
            return array(
                
    'controller' => $matches[1],
                
    'action' => 'index'
            
    );
        } elseif (
    preg_match('#^/([^/]+)/([^/]+)/([^/]+)$#'$url$matches)) {
            return array(
                
    'controller' => $matches[1],
                
    'action' => $matches[2],
                
    'param' => $matches[3]
            );
        }

        
    // if nothing else matches, then 404
        
    return array(
            
    'controller' => 'Error',
            
    'action' => 'error404'
        
    );
    }

    $routingParameters routingMatch($_SERVER['REQUEST_URI']);
    "First make it work. Then make it better."

  3. #3
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank u
    // generic urls
    // /{controllername}
    // /{controllername}/{method}/{paramname}
    if (preg_match('#^/([^/]+)$#', $url, $matches)) {
    return array(
    'controller' => $matches[1],
    'action' => 'index'
    );
    } elseif (preg_match('#^/([^/]+)/([^/]+)/([^/]+)$#', $url, $matches)) {
    return array(
    'controller' => $matches[1],
    'action' => $matches[2],
    'param' => $matches[3]
    );
    }
    i need to like /{controllername}/{paramname} or /{controllername}/{paramname}/{paramname} how could i get the parametres, can you explain regualr expression you used here.

  4. #4
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if i have the code in root folder the above code works else it take my foldername as controller. how do i identify whether the second param is method or querystringvalue

  5. #5
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if i have the code in root folder the above code works else it take my foldername as controller. how do i identify whether the second param is method or querystringvalue

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by goldensona View Post
    thank u


    i need to like /{controllername}/{paramname} or /{controllername}/{paramname}/{paramname} how could i get the parametres, can you explain regualr expression you used here.
    It's going to be tricky to have both /{controllername}/{paramname} and /{controllername}/{methodname}, because unless you define some kind of uniquely identifying format, you won't be able to distinguish between the two. Consider, for example, the fictional URL /blog/all. Is "all" a method or a param? There's really no way to tell the difference, so the kind of URL formats you want may not be possible.
    "First make it work. Then make it better."

  7. #7
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    is there any url format to identify paramname and methodname

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,578
    Mentioned
    62 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by goldensona View Post
    Hi i want to create mvc routing, i have seen some routing concept . can any one say how to implemnt

    for example :
    1) http://sitename.com/controllername?paramname -invoke index method

    2) http://sitename.com/controllername/method/paramname - invoke same controller having anohter method with the parameters pass tothe method.

    we can use ? for getting parameter values else /(Slash) for the parameter values. i m confused with it , for seo we can use / ,if i use /(slash) how can i identify which is method and which is parameter. if the user call method in controller instead of the default method(index). how could i identify the parameter value.


    Hi @goldensona ;

    One way to achieve your desired result is:

    PHP Code:
        $uri=$_SERVER['REQUEST_URI']; 
        
        if(
    '/' == $uri):
            echo 
    'No parameters, just a solitary   /  forward-slash ' ;
        endif;
           
        if(
    strpos($uri'?' )): 
            echo 
    'YES we have a ?';
        endif;
        echo 
    '<br /><br /><br />';

        echo 
    $uri;

        echo 
    $new str_replace('?''/index/'$uri);
        echo 
    '<br /><br />';

    // /*

       // to see all $_SERVER parameters
        
    echo '<pre>';
          
    $uri print_r($_SERVERtrue);
          echo 
    strlen($uri);
          echo 
    '<br />';
          echo 
    $uri;
        echo 
    '</pre>';
        die;

    // */ 
    Food for thought:
    I am only familiar with two MVC PHP Frameworks and both use Apache .htaccess's "RewriteEngine On" to route absolutely everything to index.php

    The index.php file sets paths to the framework core then activates a route.php file.

    The route.php file parses $_SERVER['REQUEST_URI'] and calls the relevant controller with the associated parameters.

    Maybe it's time to consider the above technique?


Bookmarks

Posting Permissions

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