Search code examples
phpkohana

Kohana more than one dynamic routing for specific urls


I don't know if someone else is using kohana (koseven with new name) framework for develepment. I need help about routing. I am migrating an asp site to php with using koseven ( kohana) framework and I must keep all the url routing on current site. Because of this I must use more than one routing on my project.

Url structer must be like this:

domain.com/contenttype/contentid -> contenttype is dynamic and gets data over Content Controller domain.com/profile/username ->profile is the controller and index is the action. I must get the user name from id parameter. domain.com/categories/categorname (Works fine-> categories is the controller, index is the action and categorname is the id parameter.

There is an admin page on my site and using a directory route on it.

Here is my route on bootstrap.php file:

    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',
    ]);

First Problem: If I copy kategori route for profile it uses kategori route instead of profile. Second Problem: How can I get dynamic routing for the contenttype. Content controller is the default controller and it will list the contents under the dynamic contenttype if there isn't given any content title on the id parameter. If the id parameter is identified at this time it will Show the detail of content.

Thanks.


Solution

  • 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,
        ]);