小花 3.2。 - 如何在 URI 中使用连字符
最近,我一直在研究 SEO 以及如何区别对待使用连字符或下划线的 URI,尤其是 Google,他们将连字符视为分隔符。
不管怎样,渴望调整我当前的项目以满足这个标准,我发现因为 Kohana 使用函数名称来定义页面,所以我收到了意外的“-”警告。
我想知道是否有任何方法可以在 Kohana 中使用 URI,例如:
http://www.mysite.com/controller/function-name
显然我可以为此设置一个路由处理程序...但如果我要让用户生成内容,即新闻。然后,我必须从数据库中获取所有文章,生成 URI,然后为每一篇文章进行路由。
有其他解决方案吗?
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.
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.
I was wondering whether there was any way to enable the use of URIs in Kohana like:
http://www.mysite.com/controller/function-name
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.
Are there any alternative solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
注意:这与 Laurent 的答案 中的方法相同,只是稍微更 OOP 一点。 Kohana 允许我们非常轻松地重载任何系统类,因此我们可以使用它来节省一些输入,并允许将来进行更清晰的更新。
我们可以插入 Kohana 中的请求流并修复URL 操作部分中的破折号。为此,我们将重写 Request_Client_Internal 系统类及其execute_request() 方法。在那里,我们将检查 request->action 是否有破折号,如果有,我们将它们切换为下划线,以允许 php 正确调用我们的方法。
第 1 步. 打开 application/bootstrap.php 并添加此行:
如果您需要在网址中使用下划线,则可以使用此常量在某些请求上快速禁用此功能。
步骤 2. 在:application/classes/request/client/internal.php 中创建一个新的 php 文件并粘贴此代码:
这只是替换中的所有破折号$request->action 带有下划线,因此如果 url 是 /something/foo-bar,Kohana 现在会愉快地将其路由到我们的 action_foo_bar() 方法。
同时我们用下划线屏蔽了所有的动作,以避免内容重复的问题。
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.
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.
Step 1. Open your application/bootstrap.php and add this line:
You use this constant to quickly disable this feature on some requests, if you need underscores in the url.
Step 2. Create a new php file in: application/classes/request/client/internal.php and paste this code:
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.
无法直接将带连字符的字符串映射到 PHP 函数,因此您必须进行路由。
至于用户生成的内容,您可以像 Stack Exchange 那样做。每次将用户内容保存到数据库时,都会为其生成一个 slug (
kohana-3-2-how-can-i-use-hyphens-in-uris
) 并将其与其他内容一起保存信息。然后,当您需要链接到它时,使用唯一的 id 并将 slug 附加到末尾(例如:http://stackoverflow.com/questions/7404646/kohana-3-2-how-can-i-使用连字符-in-uris
)以提高可读性。No way to directly map a hyphenated string to a PHP function so you will have to do routing.
As far as user generated content, you could do something like Stack Exchange does. Each time user content is saved to the database, generated a slug for it (
kohana-3-2-how-can-i-use-hyphens-in-uris
) and save it along with the other information. Then when you need to link to it, use the unique id and append the slug to the end (ex:http://stackoverflow.com/questions/7404646/kohana-3-2-how-can-i-use-hyphens-in-uris
) for readability.您可以使用 lambda 函数来执行此操作: http://forum.kohanaframework.org/discussion/comment /62581#Comment_62581
You can do this with lambda functions: http://forum.kohanaframework.org/discussion/comment/62581#Comment_62581
您可以执行类似的操作
,然后使用
Request::current()->param('identifier')
在函数中接收您的内容标识符,并手动解析它以查找相关数据。You could do something like
Then receive your content identifier in the function with
Request::current()->param('identifier')
and parse it manually to find the relating data.在尝试了各种解决方案后,我发现最简单、最可靠的方法是重写
Kohana_Request_Client_Internal::execute_request
。为此,请在“application\classes\kohana\request\client\internal.php”的application
文件夹中添加一个文件,然后将其内容设置为:然后添加带有连字符的操作,例如,“controller/my-action”,创建一个名为“my_action()”的操作。
如果用户尝试访问“controller/my_action”(以避免重复内容),此方法也会引发错误。
我知道有些开发人员不喜欢这种方法,但它的优点是它不会重命名操作,因此如果您检查当前操作,它将在各处一致地称为“我的操作”。使用 Route 或 lambda 函数方法,操作有时会被称为“my_action”,有时会被称为“my-action”(因为这两种方法都会重命名操作)。
After having tried various solutions, I found that the easiest and most reliable way is to override
Kohana_Request_Client_Internal::execute_request
. To do so, add a file in yourapplication
folder in "application\classes\kohana\request\client\internal.php" then set its content to:Then to add an action with hyphens, for example, "controller/my-action", create an action called "my_action()".
This method will also throw an error if the user tries to access "controller/my_action" (to avoid duplicate content).
I know some developers don't like this method but the advantage of it is that it doesn't rename the action, so if you check the current action it will be consistently called "my-action" everywhere. With the Route or lambda function method, the action will sometime be called "my_action", sometime "my-action" (since both methods rename the action).