How to have URLs with dashes in Kohana 3.x
I need to have a URL such as http开发者_如何学运维://example.com/controller/my-page-with-dashes
How can I have such as URL in Kohana? I tried creating a controller and name the action myPageWithDashes
like in the Zend Framework but that didn't work. Any idea how it should be done?
No, you just need to specify a regex parameter in your route.
Read the docs on routes, it explains this: http://kohanaframework.org/3.2/guide/kohana/routing#regex
Just as zombor said change the regex of the route:
Route:
Route::set('default', 'controller/<url>)', array('url' => '[-a-z0-9]+'))
->defaults(array(
'controller' => 'page',
'action' => 'index',
));
Controller:
Class Controller_Page {
public function action_index()
{
$url = $this->request->param('url');
}
}
array('url' => '[-a-z0-9]+')
This part changes what is allowed in the url param.
Copy the file system/classes/kohana/request/client/internal.php
to your application folder - application/classes/kohana/request/client/internal.php
. Then change line 106 from:
$action = $request->action();
to:
$action = str_replace('-', '_', $request->action());
精彩评论