質問

I would like to have my URIs like this:

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

So the URIs which would contain other pages in it would have a trailing slash at the end to be more clear to the users that it contains other pages. I tried to create a Routing class but doesn’t seem to work fine. In fact, the all layout disappear when I go to /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();
 } 

And my URI Class:

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() ? '/' : '');
 } 

And here how I would use it:

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

Any idea? Thank you!

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top