Jump to content

url routes not matching up


surreal5335

Recommended Posts

I am working on a database that sends off user entered info to a databse upon submit of the form. The problem is when this occurs preg_match() is getting weird results for its two separate string to match for a proper url route while going to a new page.

 

I have figured out what the weird behavior is:

 

the $route['url'] is just picking the url route that appears at the end of the array. I have no idea how to fix this.

Currently no info is being submitted to the database.

 

My site:

 

http://royalvillicus.com/comment_board/

 

once the data is submitted I am left with this feedback:

 

$route['url'] = /^\/(?P\d+)\/delete$/

$url = posts/create

no route found

 

$route['url'] and $url are the two strings being checked for a match. This check is being done with this code:

 

 

  // routes for all the controllers
  $routes = array(
		array('url' => '/^\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
		array('url' => '/^\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
		array('url' => '/^\/new$/', 'controller' => 'posts', 'view' => 'new'),
		array('url' => '/^\/create$/', 'controller' => 'posts', 'view' => 'create'),
		array('url' => '/^\/?$/', 'controller' => 'posts', 'view' => 'index'),
		//array('url' => '/^\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
		//array('url' => '/^\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete')
                 );
  /**
  * Return array of $_GET and $_POST data
  * @return array
  **/
  function parse_params()
  {
    $params = array();
    if(!empty($_POST))
      $params = array_merge($params, $_POST);
    if(!empty($_GET))
      $params = array_merge($params, $_GET);
    return $params;
  }

  function dispatcher($routes)
  {
    // $_SERVER['REQUEST_URI'] gets the URL typed in by the user
    $url = $_SERVER['REQUEST_URI'];
    // Removes Application root from URL
    $url = str_replace(DS.APP_ROOT.DS,'',$url);
    //Holds the Named Captures, $_POST Data
    $params = parse_params();
    // $_SERVER['QUERY_STRING'] - string in the url used to access the current page
    $url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $url);
    //Becomes true if $route['url'] matches $url
    $route_match = false;
    // loops over $routes looking for a match
    foreach($routes as $urls => $route)
    {
      
      // sets $route_match to true
      // exits loop after match found
      if(preg_match($route['url'],$url,$matches))
      {
  // if match found appends $matches to $params
        $params = array_merge($params, $matches);
        $route_match = true;
        break;
      }
    }
    // if no route matched display error
    if (!$route_match) { 
    	echo "\$route['url'] = ".$route['url']."<br />";
    	echo "\$url = ".$url."<br />";
    	exit('no route found');}
      include CONTROLLER_PATH.$route['controller'].'.php';
    if(file_exists(VIEW_PATH.'layout'.DS.$route['controller'].'.php'))
      include VIEW_PATH.'layout'.DS.$route['controller'].'.php';
    else
      include VIEW_PATH.'layout'.DS.'application.php';  
    // reset flashs back to empty
    $_SESSION['flash']['notice'] = '';
    $_SESSION['flash']['warning'] = '';
  }

 

 

I have no idea why its doing this or know where to go in order to fix it.

 

I appreciate the help

Link to comment
https://forums.phpfreaks.com/topic/199056-url-routes-not-matching-up/
Share on other sites

Because none of those patterns have psots in them, and they all use start of query asertions ( ^ ). Should be either:

 

  // routes for all the controllers
  $routes = array(
		array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
		array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
		array('url' => '/^posts\/new$/', 'controller' => 'posts', 'view' => 'new'),
		array('url' => '/^posts\/create$/', 'controller' => 'posts', 'view' => 'create'),
		array('url' => '/^posts\/?$/', 'controller' => 'posts', 'view' => 'index'),
		//array('url' => '/^posts\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
		//array('url' => '/^posts\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete')
                 );

 

or

 

  // routes for all the controllers
  $routes = array(
		array('url' => '/^[a-z]?\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
		array('url' => '/^[a-z]?\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
		array('url' => '/^[a-z]?\/new$/', 'controller' => 'posts', 'view' => 'new'),
		array('url' => '/^[a-z]?\/create$/', 'controller' => 'posts', 'view' => 'create'),
		array('url' => '/^[a-z]?\/?$/', 'controller' => 'posts', 'view' => 'index'),
		//array('url' => '/^[a-z]?\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
		//array('url' => '/^[a-z]?\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete')
                 );

 

Depending wether you explicitly want them to have to type posts.

 

Also, to increase the match probability, use a strtolower() func on the $url before you do the matching.

 

-cb-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.