簡體   English   中英

使用Kohana PHP Framework的URL中的連字符

[英]Hyphens in URLs with Kohana PHP Framework

這是一個Apache .htaccess問題。

在Kohana PHP Framework(我在3.1中),它們似乎不支持控制器或操作的URL中的連字符,這是域之后的前2個URL參數,如:

http://example.com/controller/action

要么

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

有沒有辦法可以制作我的.htaccess文件,以便我可以從控制器和動作插槽中刪除連字符(短划線),而不是其他插槽? 這是我當前的.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]

在我的項目的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擴展Controller {'開頭。
  • 然后我也能夠在URL之后傳遞3個SEO友好的參數,而不必使用更丑陋的?p1 = blah&p2 = blah&p3 = blah查詢參數技術。 這里解釋得更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM