Creating a route? Within a controller action_index

2019-07-21 03:12发布

问题:

I am working with kohana, because development goes so pretty fast. Now I want to achieve something which I can't really think of a workaround for.

What I want to achieve, there is a controller. It's called Controller_Restaurants But those restaurants, are grouped by provinces and after province is clicked they are grouped by a city, and then a list of the restaurants is shown up.

All cities and provinces are already added to there specific database with fields.

I want to create a route in my controller. So can I achieve a following link:

domain/restaurants/province/city/restaurant-name

?

Or am I thinking like a douche and should I solve this otherwise?

回答1:

Build your routing like this:

Route::set('restaurants', 'restaurants(/<province>(/<city>(/<name>))))',
    array(
        'controller' => 'restaurants',
        'action' => 'index',
    ));

It should work like this.



回答2:

It is possible to create routes inside a controller in Kohana, but it won't help you solve your problem because routing has already happened.

What you need to do is create a route with optional parameters as suggested by @kingkero:

restaurants(/<province>(/<city>(/<name>)))

Then access the parameters in the URL from the controller like so:

$province = $this->request->param('province');
...