Creating a route? Within a controller action_index

2019-07-21 03:08发布

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?

2条回答
倾城 Initia
2楼-- · 2019-07-21 03:23

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');
...
查看更多
【Aperson】
3楼-- · 2019-07-21 03:36

Build your routing like this:

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

It should work like this.

查看更多
登录 后发表回答