Welcome Guest, Not a member yet? Register   Sign In
use default controller for controller not found
#1

[eluser]fijiaaron[/eluser]
I want to treat the first uri segment as a method for the default controller if it doesn't map to a controller.

example (assuming index.php removed via rewrite):

1. -- invoke Foo controller index() method
2. [url=]http://server/foo/bar
-- invoke Foo controller bar() method
3. -- invoke default controller (Foo) index() method
4. [url=]http://server/bar
-- invoke default controller (Foo) controller bar() method unless Bar controller exists

Does this capability already exist?

If not, I'd propose creating it by extending Router. It looks like I could add this to _validate_request (and modify the logic a bit)

Code:
// Look for a method in the default controller that matches?
if ($this->routes['default_controller_method_lookup'])
{
  $methods = get_class_methods($this->default_controller);
  $public_methods = sanitize($methods); // remove constructor, private methods, etc.
  
  if (in_array($segments[0], $public_methods))
  {
    $this->set_class($this->default-controller);
    $this->set_method($segments[0]);
  }
  
}

Any comments, pitfalls?
#2

[eluser]richthegeek[/eluser]
rewrite the 404 page to redirect to Foo/bar?
#3

[eluser]fijiaaron[/eluser]
That not a very generalized solution. And it doesn't work if you have a 404 for any other reason.
#4

[eluser]richthegeek[/eluser]
sorry, I figured it would be obvious that suggestion included "if the method exists within the default controller".
#5

[eluser]fijiaaron[/eluser]
I don't follow.

Do you mean edit the function show_404() to check the default controller?
#6

[eluser]ntheorist[/eluser]
have a look at a extending router class i just posted here. It falls back like you mention http://server/bar becomes http://server/foo/bar if 'bar' is not a controller, controller sub-directory (any depth), module controller or module controller sub-directory

it may need some work but so far its working for me

the other way to do it is to manually set routes in your config
Code:
$routes['default_controller'] = 'home';

$routes['method1'] = 'home/$1';
$routes['method2'] = 'home/$1';
that can be tedious tho as you add more methods/subdirs to your site

n




Theme © iAndrew 2016 - Forum software by © MyBB