3

How can I define a route that will catch all requests and forward them to one specific controller? I've already tried adding the default route

Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
         'directory' => 'site',
         'controller' => 'foobar',
         'action' => 'foobar',
));

or

Route::set('default', '(.*)')
    -> defaults(array(
        'directory' => 'site',
        'controller' => 'foobar',
        'action' => 'foobar',
));

to my bootstrap.php, but it doesn't work. After typing localhost/a I get either

Unable to find a route to match the URI: a

or

The requested URL a was not found on this server.

error. I'm sure that the controller is valid, as

Route::set('foobar', 'foo') 
    -> defaults(array(
        'directory' => 'site',
        'controller' => 'foobar',
        'action' => 'foobar',
));

works fine.

I'm using Kohana 3.3.

Mateusz
  • 3,038
  • 4
  • 27
  • 41
  • why would you do that? that would beat the purpose of kohana's MVC – ITroubs Mar 20 '13 at 19:06
  • I'm trying to create a site-building service and I want to store all non-admin sites in a database and load them when needed. The sites are going to be added dynamically (I can't hard code the routes); I don't want them to be available only by localhost/index.php?id=7, but rather via localhost/siteName. The controller will basically do the same things for every site - load titles, meta tags, content etc. The admin sites, however, are normally routed, but I need something to access normal documents. – Mateusz Mar 20 '13 at 19:22
  • I hope my answer helps – ITroubs Mar 20 '13 at 19:35

1 Answers1

6

This should work:

Route::set('foobar', '<catcher>',array('catcher'=>'.*')) 
    -> defaults(array(
        'directory' => 'site',
        'controller' => 'foobar',
        'action' => 'foobar',
));

It <catcher> is a placeholder and array('catcher'=>'.*') defines the catcher to match the regex .*

ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • That's exactly what I was looking for, than you very much! – Mateusz Mar 20 '13 at 19:38
  • Leaving the third parameter as NULL will still match everything and skip some of the regex processing. To get the route that was caught use `Request::current()->param('catcher');` – None Jun 03 '14 at 21:27