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.

:confused:

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

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']);

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.

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

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

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.

is there any url format to identify paramname and methodname

Hi @goldensona;

One way to achieve your desired result is:


    $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($_SERVER, true);
      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?