2011-04-18 64 views
0

這是一個Apache .htaccess問題。帶有Kohana PHP框架的URL中的連字符

在Kohana的PHP框架(我是3.1),它不會出現,他們支持連字符的URL的控制器或動作,這是域後的第2個URL參數,如:

http://example.com/controller/action

OR

http://example.com/blogs/edit-article

有沒有一種辦法可以讓我的.htaccess文件,這樣我可以剝奪連字符(破折號)從控制器和動作插槽,而不是其他SLO TS?這是我目前的.htaccess文件:

Options All +FollowSymLinks -Indexes -Multiviews 

# Turn on URL rewriting 
RewriteEngine On 

# Installation directory 
RewriteBase/

# Protect hidden files from being viewed 
<Files .*> 
    Order Deny,Allow 
    Deny From All 
</Files> 

RewriteRule ^assets/(.*)$ application/views/assets/$1 

RewriteCond %{REQUEST_FILENAME} !/application/views/assets/.* 

# Protect application and system files from being viewed 
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L] 

# Allow any files or directories that exist to be displayed directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Rewrite all other URLs to index.php/URL 
RewriteRule .* index.php/$0 [PT] 
+0

另請參閱http://stackoverflow.com/questions/7404646#answer-8751122 – ChrisV 2012-10-14 16:42:58

回答

2

在Kohana中3.1的boostrap.php我的項目,我不得不添加此默認路由上面:

Route::set(
    'custom', 
    function($uri) { 
     $uri = rtrim($uri, '/'); 
     $asParts = @ explode('/',$uri); 
     $controller = @ $asParts[0]; 
     $action = @ $asParts[1]; 
     $param1 = @ $asParts[2]; 
     $param2 = @ $asParts[3]; 
     $param3 = @ $asParts[4]; 
     $controller = str_replace('-','_',$controller); 
     $action = str_replace('-','_',$action); 
     $controller = (empty($controller)) ? 'home' : $controller; 
     $action = (empty($action)) ? 'index' : $action; 
     return array(
      'controller' => $controller, 
      'action' => $action, 
      'param1' => $param1, 
      'param2' => $param2, 
      'param3' => $param3 
     ); 
    } 
); 

這讓我做以下事情:

  • 動作中的破折號變爲控制器類中帶有下劃線的函數。所以,'add-new'變成'action_add_new()'。
  • 控制器中的破折號變成子文件夾,因爲控制器在kohana中自然強調錶示子文件夾。所以,由於控制器上面的str_replace()函數,如果我有'test1-test2'的控制器,Kohana會去查找一個控制器文件夾'test1',然後是一個控制器文件'test2.php'。但問題是,你的test2.php需要以'Controller_Test1_Test2 extends Controller {'開始。
  • 然後,我也能夠通過URL後的3個友好的參數,而不必使用更醜陋?p1 = blah & p2 = blah & p3 =等值查詢參數技術。這是explained more here