【问题标题】:Kohana:: routing with infinity paramsKohana:: 无限参数路由
【发布时间】:2012-07-07 14:22:34
【问题描述】:

我想在控制器的操作中使用 url 参数,比如方法参数(比如在 CodeIgniter 中)。我想为 UNLIMITED 参数数量(0、5、10 ...)进行路由。

url: http://localhost/controller/action/param1/param2/..../param10...

行动将是:

function action_something($param1, $param2, .... $param10) { ... }

有可能吗?我有一个简单的应用程序,我想为每种情况都有一个默认路由..

【问题讨论】:

  • 检查这个:kohanaframework.org/3.0/guide/kohana/routing#regexRoute::set('default', '(<controller>(/<action>(/<stuff>)))', array('stuff' => '.*')) ->defaults(array( 'controller' => 'welcome', 'action' => 'index', ));

标签: php routing kohana


【解决方案1】:

您可以通过在 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;
    }

}

用法:

然后,如果您有一个 URL,例如 http://example.com/controller/action/param1/value1/param2/value2...。您可以从控制器UrlParam::get($this, 'param1') 调用以获取“param1”的值等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多