Search code examples
phprouteskohana-3kohana-3.2

Kohana 3.2 controllers in classes/controllers/<subfolder>/<subfolder>


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.


Solution

  • 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.