简体   繁体   English

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

[英]Hyphens in URLs with Kohana PHP Framework

This is an Apache .htaccess question. 这是一个Apache .htaccess问题。

In Kohana PHP Framework (I'm on 3.1), it doesn't appear that they support hyphens in URLs for the controller or action, which are the first 2 URL parameters after the domain, as in: 在Kohana PHP Framework(我在3.1中),它们似乎不支持控制器或操作的URL中的连字符,这是域之后的前2个URL参数,如:

http://example.com/controller/action http://example.com/controller/action

OR 要么

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

Is there a way I can make my .htaccess file so that I can strip hyphens (dashes) out of the controller and action slots, but not the other slots? 有没有办法可以制作我的.htaccess文件,以便我可以从控制器和动作插槽中删除连字符(短划线),而不是其他插槽? Here's my current .htaccess file: 这是我当前的.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]

In Kohana 3.1's boostrap.php for my project, I had to add this above the default route: 在我的项目的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
        );
    }
);

This lets me do the following things: 这让我可以做以下事情:

  • A dash in the action becomes a function in the controller class with an underscore. 操作中的破折号成为控制器类中带有下划线的函数。 So, 'add-new' becomes 'action_add_new()'. 因此,'add-new'变为'action_add_new()'。
  • A dash in the controller becomes a subfolder because controller underscores naturally in kohana mean a subfolder. 控制器中的破折号成为子文件夹,因为控制器在kohana中自然地下划线表示子文件夹。 So, because of the str_replace() function above on the controller, if I have a controller of 'test1-test2', Kohana goes looking for a controller folder 'test1', and then a controller file 'test2.php'. 因此,由于控制器上面的str_replace()函数,如果我有一个'test1-test2'控制器,Kohana会寻找控制器文件夹'test1',然后是控制器文件'test2.php'。 But the catch is this, your test2.php needs to begin as 'class Controller_Test1_Test2 extends Controller {'. 但问题是,你的test2.php需要以'类Controller_Test1_Test2扩展Controller {'开头。
  • And then I'm also able to pass 3 SEO-friendly parameters after the URL without having to use the more ugly ?p1=blah&p2=blah&p3=blah query param technique. 然后我也能够在URL之后传递3个SEO友好的参数,而不必使用更丑陋的?p1 = blah&p2 = blah&p3 = blah查询参数技术。 This is explained more here . 这里解释得更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM