1

I have tried the following way

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

Instead of Home controller in Campaign folder I need to load Home Controller from Campaign/City folder by default. I have used the above code in bootstrap.php, but it gives 'URL not found on this server' error

PravinS
  • 2,640
  • 3
  • 21
  • 25
Harshala
  • 21
  • 7

1 Answers1

0

The array that is passed as the third parameter to Route::set() restricts the values that can be passed to the route. In your code array('directory' => '(admin|affiliate)') restricts the directory parameter to be either 'admin' or 'affiliate' To have it go deeper you would need to modify the route.

The Kohana Routing Guide has a bunch of examples using filters to route in any way you could possibly imagine, but you could route to subdirectories without turning to filters.

For example, with the following directory structure:

classes/Controller/
  Admin/
    Cupertino/
      Home.php (Controller_Admin_Cupertino_Home)
    Home.php (Controller_Admin_Home)
  Affiliate/
    Cupertino/
      Home.php (Controller_Affiliate_Cupertino_Home)
    Home.php (Controller_Affiliate_Home)

And the following route:

Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
    array(
        'directory' => '(admin/cupertino|admin|affiliate/cupertino|affiliate)'
    ))
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

The URLs index.php/admin, index.php/admin/cupertino, index.php/affiliate, and index.php/affiliate/cupertino will route through their respective controllers.

Subdirectories need to be listed before their parents otherwise Kohana will always match to the parent. e.g. the following will always route to Controller_Admin_Home even for the URL index.php/admin/cupertino:

`array('directory' => 'admin|admin/cupertino')`.

Using filters might look something like the following:

Route::set('admin_subsections', 'admin/<directory>(/<controller>(/<action>(/<id>)))',
    array(
        'directory' => '(cupertino|sanjose|santacruz)'
    ))
    ->filter(function($route, $params, $request)
    {
        // append "admin/" to the directory param
        $params['directory'] = 'admin/' . $params['directory'];
        return $params; // Returning an array will replace the parameters
    })
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

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

And again, order matters.

None
  • 5,491
  • 1
  • 40
  • 51
  • Thank you for the reply The url I need is camp/cities The first solution you provided is that if user types admin, he will go to admin/home/index.php and if he writes admin/cupertino, he will go to admin/cupertino/home/index.php So the question is, does he have to place home controller in both admin and cupertino folder? Mine is the same case, user can type camp or camp/cities, yet he should be redirected to camp/cities/home/index.php – Harshala May 12 '14 at 05:04
  • `` specifies where to look for a controller. If you want all requests to use the same controller, e.g. `Controller_Home`, then the directory parameter is the wrong parameter to use in the route because it has built in meaning in Kohana. You could change `` to a different param name such as `` and then the controller would be looked for in `classes/controller` instead of in a sub directory. If you haven't yet, look at the Routing docs I linked to in my response and the Route class. This can be worked out by reading the simplistic and unintimidating Kohana code. – None May 13 '14 at 14:21