Вопрос

In Kohana 2, a controller function could have arguments in it without needing to write a route for it.

url: /some/arg/is/here

and in the controller, i could simply have four args, of any name, and they'd be automatically accessible from within the function.

public function myFunc($a, $b, $c, $d) {}

but in Kohana 3, I have to go write a route for type of route I want to have. Is there a route I can use that will make my url and args play nice with each other without me having to do extra work each time i write a new function?

Это было полезно?

Решение

All args must be specified in the route, but can be made optional. In this instance you may want to change the default route to something like...

Route::set('default', '(<controller>(/<action>(/<arg1>(/<arg2>(/<arg3>(/<arg4>))))))');

You can then reference the args by using "request"...

$this->request->param('arg1');
$this->request->param('arg2');
$this->request->param('arg3');
$this->request->param('arg4');

You could obviously have more than 4 if you needed them.

Другие советы

FYI Kohana 3 is a complete rewrite from scratch. Kohana 2 and 3 could be considered two separate frameworks.

As for the route, you maybe want to use a catch-all route. Check it out here Kohana 3.3 catch-all route

I would advise against using it, because it looses the whole purpose of KO3 routing flexibility.

Also, you can't access parameters as method arguments anymore since KO 3.1 (I think). Instead use the Request class to retrieve the parameters like so: $this->request->param('abc'); or instantiate the Request class if you're using it outside the controller like so Request::current()->param('abc');

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top