Kohana 3.2. -如何在URI中使用连字符 [英] Kohana 3.2. - How can I use hyphens in URIs

查看:103
本文介绍了Kohana 3.2. -如何在URI中使用连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在进行SEO的研究,以及对使用连字符或下划线的URI的区别对待,特别是Google将连字符视为分隔符的情况.

Recently I've been doing some research into SEO and how URIs that use hyphens or underscores are treated differently, particularly by Google who view hyphens as separators.

无论如何,渴望适应我当前的项目以符合此标准,我发现由于Kohana使用函数名来定义页面,所以我收到了意外的-"警告.​​

Anyway, eager to adapt my current project to meet this criteria I found that because Kohana uses function names to define pages I was receiving the unexpected '-' warning.

我想知道是否有任何方法可以在Kohana中启用URI,如:

I was wondering whether there was any way to enable the use of URIs in Kohana like:

http://www.mysite.com/controller/function-name

很显然,我可以为此设置一个routeHandler……但是如果我要让用户生成内容(即新闻).然后,我必须从数据库中获取所有文章,生成URI,然后为每个文章进行路由.

Obviously I could setup a routeHandler for this... but if I was to have user generated content, i.e. news. I'd then have to get all articles from the database, produce the URI, and then do the routing for each one.

还有其他解决方案吗?

推荐答案

注意:这与 Laurent's答案,仅是面向对象操作. Kohana允许非常轻松地重载任何系统类,因此我们可以使用它来节省一些输入,并允许将来进行更干净的更新.

Note: This is the same approach as in Laurent's answer, just slightly more OOP-wise. Kohana allows one to very easily overload any system class, so we can use it to save us some typing and also to allow for cleaner updates in the future.

我们可以插入Kohana中的请求流,并在URL的操作部分中修复破折号.为此,我们将重写Request_Client_Internal系统类,它是execute_request()方法.在那里,我们将检查request-> action是否包含破折号,如果是,我们将其切换为下划线以允许php正确调用我们的方法.

We can plug-in into the request flow in Kohana and fix the dashes in the action part of the URL. To do it we will override Request_Client_Internal system class and it's execute_request() method. There we'll check if request->action has dashes, and if so we'll switch them to underscores to allow php to call our method properly.

第1步.打开您的 application/bootstrap.php 并添加以下行:

Step 1. Open your application/bootstrap.php and add this line:

define('URL_WITH_DASHES_ONLY', TRUE);

如果需要在URL中加下划线,则可以使用此常量在某些请求上快速禁用此功能.

You use this constant to quickly disable this feature on some requests, if you need underscores in the url.

第2步. application/classes/request/client/internal.php 中创建一个新的php文件,并粘贴以下代码:

Step 2. Create a new php file in: application/classes/request/client/internal.php and paste this code:

<?php defined('SYSPATH') or die('No direct script access.');

class Request_Client_Internal extends Kohana_Request_Client_Internal {

    /**
     * We override this method to allow for dashes in the action part of the url
     * (See Kohana_Request_Client_Internal::execute_request() for the details)
     *
     * @param   Request $request
     * @return  Response
     */
    public function execute_request(Request $request)
    {
        // Check the setting for dashes (the one set in bootstrap.php)
        if (defined('URL_WITH_DASHES_ONLY') and URL_WITH_DASHES_ONLY == TRUE) 
        {
            // Block URLs with underscore in the action to avoid duplicated content
            if (strpos($request->action(), '_') !== false)
            {
                throw new HTTP_Exception_404('The requested URL :uri was not found on this server.', array(':uri' => $request->uri()));
            }

            // Modify action part of the request: transform all dashes to underscores
            $request->action( strtr($request->action(), '-', '_') );
        }
        // We are done, let the parent method do the heavy lifting
        return parent::execute_request($request);
    }

} // end_class Request_Client_Internal

这只是将$ request-> action中的所有破折号替换为下划线,因此,如果url为/something/foo-bar ,那么Kohana现在会很乐意将其路由到我们的action_foo_bar( ) 方法.

What this does is simply replacing all the dashes in the $request->action with underscores, thus if url was /something/foo-bar, Kohana will now happily route it to our action_foo_bar() method.

同时,我们用下划线阻止所有操作,以避免重复的内容问题.

In the same time we block all the actions with underscores, to avoid the duplicated content problems.

这篇关于Kohana 3.2. -如何在URI中使用连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆