nkuhta
nkuhta

Reputation: 11128

Kohana:: routing with infinity params

I want to take url params in controller's action, like method params (like in CodeIgniter). I want to have routing for UNLIMITED params amount (0, 5, 10 ...).

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

And action will be:

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

Is it possible? I have simple application, and I want to have one default routing for every cases..

Upvotes: 1

Views: 367

Answers (1)

laurent
laurent

Reputation: 90776

You can achieve that by adding an "overflow" route to your bootstrap.php file:

Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?'))
    ->defaults(array(
        'controller' => 'api',
        'action'     => 'index',
    ));

Then I usually use this kind of class to access the various parameters:

<?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;
    }

}

Usage:

Then if you have a URL such as http://example.com/controller/action/param1/value1/param2/value2.... You can call from the controller UrlParam::get($this, 'param1') to get the value of "param1", etc.

Upvotes: 3

Related Questions