Kohana ::无限参数路由
问题描述:
我想在控制器的action中使用url params,就像方法params(就像在CodeIgniter中一样)。我想要路由无限 params金额(0,5,10 ...)。Kohana ::无限参数路由
url: http://localhost/controller/action/param1/param2/..../param10...
和行动将是:
function action_something($param1, $param2, .... $param10) { ... }
这可能吗?我有简单的应用程序,我想对每一个案件一个缺省路由..
答
可以实现通过添加一个“溢出”的途径获得的bootstrap.php文件:通常
Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
->defaults(array(
'controller' => 'api',
'action' => 'index',
));
然后我使用这种类以访问各种参数:
<?php defined('SYSPATH') or die('No direct script access.');
class UrlParam {
static public function get($controller, $name) {
$output = $controller->request->param($name);
if ($output) return $output;
$overflow = $controller->request->param("overflow");
if (!$overflow) return null;
$exploded = explode("/", $overflow);
for ($i = 0; $i < count($exploded); $i += 2) {
$n = $exploded[$i];
if ($n == $name && $i < count($exploded) - 1) return $exploded[$i + 1];
}
return null;
}
}
用法:
那么如果y您有一个网址,如http://example.com/controller/action/param1/value1/param2/value2...
。您可以从控制器UrlParam::get($this, 'param1')
调用以获取“param1”等的值。
选中此项:http://kohanaframework.org/3.0/guide/kohana/routing#regex Route :: set('default' , '((/ (/ )))',阵列( '东西'=> '*')) - >默认值(阵列( '控制器'=> '欢迎', '动作'= >'index', ));' –
Chandu
2012-07-07 14:30:17