最近我一直在做一些关于SEO的研究,以及使用连字符或下划线的uri是如何被区别对待的,特别是谷歌将连字符视为分隔符。
无论如何,我急切地想调整我当前的项目来满足这个标准,我发现因为Kohana使用函数名来定义页面,我收到了意想不到的'-'警告。
我想知道是否有任何方法可以在Kohana中使用uri,如:
http://www.mysite.com/controller/function-name
显然我可以为这个设置一个routeHandler…但如果我有用户生成的内容,即新闻。然后,我必须从数据库中获取所有文章,生成URI,然后为每一篇文章做路由。
有其他解决方案吗?
注意:这与Laurent的回答中的方法相同,只是稍微偏重于oop。Kohana允许很容易地重载任何系统类,所以我们可以使用它来节省一些输入,也允许在将来进行更干净的更新。
我们可以插入到Kohana的请求流中,并修复URL的操作部分中的破折号。为此,我们将重写Request_Client_Internal系统类及其execute_request()方法。在这里,我们将检查request->action是否有破折号,如果有,我们将它们切换为下划线,以允许php正确调用我们的方法。
步骤1。打开应用程序/bootstrap.php并添加以下行:
define('URL_WITH_DASHES_ONLY', TRUE);
如果你需要url中的下划线,你可以使用这个常量在一些请求中快速禁用这个特性。
步骤2。在application/classes/request/client/internal.php创建一个新的php文件,并粘贴以下代码:
<?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()方法。
同时我们用下划线把所有的动作都封住,以避免重复的内容问题。
无法将连字符字符串直接映射到PHP函数,因此您必须进行路由。
对于用户生成的内容,您可以做一些类似Stack Exchange的事情。每次将用户内容保存到数据库时,为其生成一个段(kohana-3-2-how-can-i-use-hyphens-in-uris
),并将其与其他信息一起保存。然后,当您需要链接到它时,使用唯一的id并在末尾添加段塞(例如:http://stackoverflow.com/questions/7404646/kohana-3-2-how-can-i-use-hyphens-in-uris
)以提高可读性。
您可以使用lambda函数:http://forum.kohanaframework.org/discussion/comment/62581#Comment_62581
你可以这样做
Route::set('route', '<controller>/<identifier>', array(
'identifier' => '[a-zA-Z'-]*'
))
->defaults(array(
'controller' => 'Controller',
'action' => 'show',
));
然后用Request::current()->param('identifier')
接收函数中的内容标识符,并手动解析以查找相关数据
在尝试了各种解决方案后,我发现最简单,最可靠的方法是覆盖Kohana_Request_Client_Internal::execute_request
。为此,在"application'classes'kohana'request'client'internal.php"的application
文件夹中添加一个文件,然后将其内容设置为:
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_Request_Client_Internal extends Request_Client {
/**
* @var array
*/
protected $_previous_environment;
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @param Request $request
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
* @deprecated passing $params to controller methods deprecated since version 3.1
* will be removed in 3.2
*/
public function execute_request(Request $request)
{
// Create the class prefix
$prefix = 'controller_';
// Directory
$directory = $request->directory();
// Controller
$controller = $request->controller();
if ($directory)
{
// Add the directory name to the class prefix
$prefix .= str_replace(array('''', '/'), '_', trim($directory, '/')).'_';
}
if (Kohana::$profiling)
{
// Set the benchmark name
$benchmark = '"'.$request->uri().'"';
if ($request !== Request::$initial AND Request::$current)
{
// Add the parent request uri
$benchmark .= ' « "'.Request::$current->uri().'"';
}
// Start benchmarking
$benchmark = Profiler::start('Requests', $benchmark);
}
// Store the currently active request
$previous = Request::$current;
// Change the current request to this request
Request::$current = $request;
// Is this the initial request
$initial_request = ($request === Request::$initial);
try
{
if ( ! class_exists($prefix.$controller))
{
throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()));
}
// Load the controller using reflection
$class = new ReflectionClass($prefix.$controller);
if ($class->isAbstract())
{
throw new Kohana_Exception('Cannot create instances of abstract :controller',
array(':controller' => $prefix.$controller));
}
// Create a new instance of the controller
$controller = $class->newInstance($request, $request->response() ? $request->response() : $request->create_response());
$class->getMethod('before')->invoke($controller);
// Determine the action to use
/* ADDED */ if (strpos($request->action(), '_') !== false) throw new HTTP_Exception_404('The requested URL :uri was not found on this server.', array(':uri' => $request->uri()));
/* MODIFIED */ $action = str_replace('-', '_', $request->action()); /* ORIGINAL: $action = $request->action(); */
$params = $request->param();
// If the action doesn't exist, it's a 404
if ( ! $class->hasMethod('action_'.$action))
{
throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()));
}
$method = $class->getMethod('action_'.$action);
$method->invoke($controller);
// Execute the "after action" method
$class->getMethod('after')->invoke($controller);
}
catch (Exception $e)
{
// Restore the previous request
if ($previous instanceof Request)
{
Request::$current = $previous;
}
if (isset($benchmark))
{
// Delete the benchmark, it is invalid
Profiler::delete($benchmark);
}
// Re-throw the exception
throw $e;
}
// Restore the previous request
Request::$current = $previous;
if (isset($benchmark))
{
// Stop the benchmark
Profiler::stop($benchmark);
}
// Return the response
return $request->response();
}
} // End Kohana_Request_Client_Internal
然后添加一个带有连字符的动作,例如"controller/my-action",创建一个名为"my_action()"的动作。
如果用户试图访问"controller/my_action"(以避免重复内容),该方法也会抛出错误。
我知道有些开发人员不喜欢这个方法,但它的优点是它不重命名动作,所以如果你检查当前的动作,它将始终被称为"my-action"到处。对于Route或lambda函数方法,动作有时被称为"my_action",有时被称为"my-action"(因为这两种方法都重命名了动作)。