3

I already saw many questions that are quite similar to this question (such as this one and this), but my problem is I have my controllers in a subfolder within a folder inside the controllers folder. My directory structure looks like this:

classes/
    controllers/
        admin/
            manageMemberProfile/
                memberList.php
                memberProfileInfo.php
                editMemberProfile.php
            manageCompanyProfile/
                ........
        member/
            ........

        guest/
            ........

    models/
        ........

Please take note that I've already done the solution in the link I provided(and managed to make it work) but its just for controllers that are in a folder inside controllers folder. What I want is to call my controllers with this kind of directory setup. Im quite new to routing in kohana 3.2, so I really dont know how to solve this, and I also read their documentation about routing but I still cant solve this problem of mine.

Community
  • 1
  • 1
Alex Coroza
  • 1,747
  • 3
  • 23
  • 39

1 Answers1

1

The answers stated in the links work here as well. You just need to add the subdirectory, e.g. like this

Route::set('admin_manageMembersProfile', 'admin/manageMembersProfile(/<controller>)')
    ->defaults(array(
        'directory' => 'admin/manageMembersProfile',
        'controller' => 'defaultController',
        'action' => 'defaultAction',
    ));

Of course it will be stressfull to do this for every subdirectory. So you could make use of the Lambda/Callback route logic:

Route::set('admin', function($uri) {
    $directories = array('manageMembersProfile', 'manageOthers');
    if (preg_match('#^admin/('.implode('|', $directories).')(/[^/]+)*#i', $uri, $match)) {
        $subdirectory = $match[1];
        if (array_key_exists(2, $match)) {
            $controller = trim($match[2], '/');
        } else {
            $controller = 'defaultController';
        }
        if (array_key_exists(3, $match)) {
            $action = trim($match[3], '/');
        } else {
            $action = 'defaultAction';
        }
        return array(
            'directory' => 'admin/'.$subdirectory,
            'controller' => $controller,
            'action' => $action,
        );
    }
});

This is only a very basic example but I hope it shows how you can handle routing this way.

kero
  • 10,647
  • 5
  • 41
  • 51
  • Do I have to change my controller name into "Controller_Guest_MemberProfileInfo" ? Because Im trying your first solution but its still not working – Alex Coroza Jun 28 '15 at 14:52
  • 1
    @boi_echos In the admin example, a controller would need to be named `Controller_Admin_ManageMembersProfile_MemberList` - every subdirectory needs to be added with `_` – kero Jun 28 '15 at 14:54
  • Wow its now working. Thanks. I had a problem in calling an action in a controller and I've found out that you have to add "(/)" in the route. – Alex Coroza Jun 28 '15 at 15:14