Kohana 针对特定网址的多个动态路由

Kohana more than one dynamic routing for specific urls

提问人:Halit YILMAZ 提问时间:5/1/2020 更新时间:5/24/2020 访问量:202

问:

我不知道是否有其他人正在使用 kohana(带有新名称的 koseven)框架进行开发。我需要有关路由的帮助。我正在使用 koseven ( kohana) 框架将 asp 站点迁移到 php,并且我必须将所有 url 路由保留在当前站点上。因此,我必须在我的项目中使用多个路由。

Url structer 必须是这样的:

domain.com/contenttype/contentid -> contenttype 是动态的,并通过 Content Controller 获取数据 domain.com/profile/username ->profile 是控制器,index 是操作。我必须从 id 参数中获取用户名。 domain.com/categories/categorname (Works fine-> categories 是控制器,index 是操作,categorname 是 id 参数。

我的网站上有一个管理页面,并在其上使用目录路由。

这是我在引导.php文件上的路线:

    Route::set('panel', '<directory>(/<controller>(/<action>(/<id>)))', array('directory' => 'panel'))
        ->defaults(array(
            'controller' => 'panel',
            'action' => 'index',
        ));
Route::set('kategori','<kategori>(/<id>)', array('id'=>'.*'))
    ->defaults([
        'controller'=>'kategori',
        'action'=>'index',
    ]);

Route::set('default', '(<controller>(/<action>(/<id>)))', array('id'=>'.*'))
    ->defaults([
        'controller' => 'anasayfa',
        'action'     => 'index',
    ]);

第一个问题:如果我复制 kategori 路由作为配置文件,它使用 kategori 路由而不是配置文件。 第二个问题:如何获取 contenttype 的动态路由。内容控制器是默认控制器,如果 id 参数上未指定任何内容标题,它将列出动态 contenttype 下的内容。如果此时标识了 id 参数,则会显示内容的详细信息。

谢谢。

php kohana

评论


答:

0赞 WinterSilence 5/24/2020 #1
Route::set('panel', 'panel(/<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
    ->defaults(array(
        'directory'  => 'panel',
        'controller' => 'dashboard',
        'action'     => 'index',
        'id'         => null,
    ));

Route::set('kategori','<kategori>(/<id>)', ['kategori' => '[-\w]+', 'id' => '[-\w]+'])
    ->defaults([
        'controller' => 'kategori',
        'action' => 'index',
        'id' => null,
    ])
    ->filter(function ($route, $params, $request) {
        $model = ORM::factory('Kategori', ['kategori' => $params['kategori'], 'id' => $params['id']]);
        if ($model->loaded()) {
             $params['model'] = $model;
             return $params;
        }
        return false;
    });

Route::set('default', '(<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
    ->defaults([
        'controller' => 'anasayfa',
        'action'     => 'index',
        'id'         => null,
    ]);