tags:

views:

108

answers:

1

I am setting up a simple routing system for my new custom MVC framework that I am making.

Currently my router class views the URL as such:

www.example.com/controller/controller_action/some/other/params

So, essentially...i've been reserving the first two segments of the URI for controller routing. However, what if I just want to run the following?

www.example.com/controller/some/other/params

...which would attempt to just run the default controller action and send the extra params to it?

Here is the simple router im using:

    \* --- htaccess --- *\
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

    \* --- index.php --- *\
    if(array_key_exists('rt',$_GET)) {
        $path = $_GET['rt'];
        $uri = explode('/',$this->path);
        if(empty($uri[0])) {
     $load->ctrl('home');
        }elseif(empty($uri[1])) {
     $load->ctrl($uri[0]);
        }else{
     $load->ctrl($uri[0],$uri[1]);
        }
    }else{
        $load->ctrl('index');
    }

    \* --- loader class --- *\
    public function ctrl($ctrl,$action=null) {
        $ctrl_name = 'Ctrl_'.ucfirst(strtolower($ctrl));
        $ctrl_path = ABS_PATH . 'ctrl/' . strtolower($ctrl) . '.php';
        if(file_exists($ctrl_path)) { require_once $ctrl_path;}

        $ctrl = new $ctrl_name();

        is_null($action) ? $action = "__default" : $action = strtolower($action);
        $ctrl->$action();
    }

Any help/comments would be awesome, thanks!

+1  A: 

You could handle this within your controller. Typically, MVC frameworks will call a default method when the requested method isn't available. Simply overwrite this fallback-method to call your desired method and pass the parameter list in as parameters.

For instance, KohanaPHP has the __call($method, $params) method that is called when the requested method doesn't exist. You could handle the logic within this, or its functional equivalent in your MVC framework..

This would let you keep the logic internal to the controller itself rather than having it blasted out between various files.

Jonathan Sampson
i THINK i get what oyu mean...do you mean, if i have a index() method in my controller that gets called upon no supplied method, or no method found - bank on having that method called because the params won't match it, and then chop the URI apart in that method?
johnnietheblack
What I mean is you should have a missingMethod() method that gets called when a non-existent method is requested. In Kohana, the requested method is passed in as the first parameter, and the provided parameters follow. Just patch these through to your index() method, and you should be all set.
Jonathan Sampson
you edited right before i submitted my comment...and you answered my question, i think...hrm, im iffy on it, but im so taken back by diving into this framework, that its not hard to do right now:) its probably a great idea haha
johnnietheblack
I'm thinking about the structure. This method would allow you to keep your controller-logic within your controller :)
Jonathan Sampson