Kohana PHP 框架中 URL 中的连字符

发布于 2024-11-02 09:45:19 字数 1184 浏览 1 评论 0 原文

这是一个 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]

This is an Apache .htaccess question.

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:

http://example.com/controller/action

OR

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? Here's my current .htaccess file:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-11-09 09:45:19

在我的项目的 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 需要以“class Controller_Test1_Test2 extends Controller {”开头。
  • 然后我还可以在 URL 之后传递 3 个 SEO 友好的参数,而不必使用更难看的 ?p1=blah&p2=blah&p3=blah 查询参数技术。这是这里解释了更多

In Kohana 3.1's boostrap.php for my project, I had to add this above the default route:

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()'.
  • A dash in the controller becomes a subfolder because controller underscores naturally in kohana mean a subfolder. 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'. But the catch is this, your test2.php needs to begin as 'class Controller_Test1_Test2 extends 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. This is explained more here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文