URI在CodeIgniter中使用尾部斜杠进行路由

时间:2014-03-06 15:36:06

标签: php codeigniter routing

我想让我的URI像这样:

page
module/        -> point to module/index(trailing slash at the end)
module/page1   -> point to module/page1
module/page2   -> etc..

因此,包含其他页面的URI在末尾会有一个尾部斜杠,以便用户更清楚它包含其他页面。我试图创建一个路由类,但似乎没有正常工作。事实上,当我转到/ module /:

时,所有布局都会消失
function _parse_routes()
 {
  // Turn the segment array into a URI string
  $uri = implode('/', $this->uri->segments);

  // Does our URI actually have a trailing slash?
  if($this->uri->has_trailing_slash()) {
   $uri .= '/';
  }

  // Is there a literal match?  If so we're done
  if (isset($this->routes[$uri]))
  {
   return $this->_set_request(explode('/', $this->routes[$uri]));
  }

  // Loop through the route array looking for wild-cards
  foreach ($this->routes as $key => $val)
  {
   // Convert wild-cards to RegEx
   $key = str_replace(':any', '.*[^\/]{1}', str_replace(':num', '[0-9]+', $key));

   // Does the RegEx match?
   if (preg_match('#^'.$key.'$#', $uri))
   {
    // Do we have a back-reference?
    if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
    {
     $val = preg_replace('#^'.$key.'$#', $val, $uri);
    }

    return $this->_set_request(explode('/', $val));
   }
  }

  // If we got this far it means we didn't encounter a
  // matching route so we'll set the site default route
  $this->_set_request($this->uri->segments);
 }

 protected function has_trailing_slash() {
  return $this->uri->has_trailing_slash();
 } 

我的URI类:

private function _detect_uri()
 {
  if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
  {
   return '';
  }

  $uri = $_SERVER['REQUEST_URI'];
  if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
  {
   $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
  }
  elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
  {
   $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
  }

  // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
  // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
  if (strncmp($uri, '?/', 2) === 0)
  {
   $uri = substr($uri, 2);
  }
  $parts = preg_split('#\?#i', $uri, 2);
  $uri = $parts[0];
  if (isset($parts[1]))
  {
   $_SERVER['QUERY_STRING'] = $parts[1];
   parse_str($_SERVER['QUERY_STRING'], $_GET);
  }
  else
  {
   $_SERVER['QUERY_STRING'] = '';
   $_GET = array();
  }

  if ($uri == '/' || empty($uri))
  {
   return '/';
  }

  $uri = parse_url($uri, PHP_URL_PATH);

  // Does a trailing slash exist?
  if(preg_match('/\/$/', $uri))
  {
   $this->has_trailing_slash = TRUE;
  }

  // Do some final cleaning of the URI and return it
  return str_replace(array('//', '../'), '/', trim($uri, '/'));
 }

 /**
  * Returns whether or not the URI loaded with trailing slash
  *
  * @access public
  * @return string
  */
 public function has_trailing_slash()
 {
  return $this->has_trailing_slash;  
 }

 function uri_string()
 {
  return $this->uri_string() . ($this->has_trailing_slash() ? '/' : '');
 } 

在这里我将如何使用它:

$this->layout->view('users/messaging/index', $data);

有什么想法吗?谢谢!

0 个答案:

没有答案