Вопрос

How do I correctly setup routes in Kohana 3.3 where the name of my controller and directory are the same as in my example below?

/application/classes/Controller/Admin/Author.php
 - admin/author
 - admin/author/add
 - admin/author/edit

/application/classes/Controller/Admin/Author/Book.php
 - admin/author/book
 - admin/author/book/add
 - admin/author/book/edit

When using the following routes in the specified order, I'm only able to access admin/author{/action}, but not admin/author/book{/action}.

Reversing the routing order gives me access to admin/author/book{/action}, but not admin/author{/action}

Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin',
        'controller' => 'Main',
        'action'     => 'index',
    ));

Route::set('admin/author', 'admin/author(/<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin/author',
        'controller' => 'Main',
        'action'     => 'index',
    ));
Это было полезно?

Решение

You need something like this:

Route::set('admin-author', '<directory>/<controller>(/<action>(/<id>))', array(
        'directory' => '(admin|admin/author)',
        'action'    => '(add|edit|delete|index)'
    ))
    ->defaults(array(
        'directory'  => 'admin',
        'controller' => 'author',
    ));

Also, you can try to check action with regex ^book (not tested).

Another way is to use Route filters.

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

you need to define for admin/author/book as well

Route::set('admin-author-book', 'admin/author(/<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin/author',
        'controller' => 'book',
        'action'     => 'index',
    ));
Route::set('admin-author-book', 'admin/author(/book(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin/author',
        'controller' => 'book',
        'action'     => 'index',
   ));

The most important thing about routes: Routes are matched in the order they are added. Reversing the order of your routes was correct, but the problem is that your route admin/author also matches admin/author{/action}. What you could do: make the controller and action mandatory:

// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>/<action>(/<id>)')
    ->defaults(array(
        'directory'  => 'admin/author',
    ));

Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin',
        'controller' => 'Main',
        'action'     => 'index',
    ));

There are several approaches, another one would be to create a "whitelist" of controllers:

// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>(/<action>(/<id>))',
    array(
        'controller' => '(book|another-controller)'
    ))
    ->defaults(array(
        'directory'  => 'admin/author',
    ));

Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'directory'  => 'admin',
        'controller' => 'Main',
        'action'     => 'index',
    ));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top