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 parameters to it?

Here is the simple router I'm 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();
    }

How can I do this?

2
  • Which MVC framework are you using? Commented Dec 8, 2009 at 5:12
  • @Jonathan, as mentioned in the question, im using a custom framework (learning for the sake of learning)...i can therefore choose whatever i'd like.i know there must be a simple way of getting around this problem, im just missing it... thanks! Commented Dec 8, 2009 at 5:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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
I'm thinking about the structure. This method would allow you to keep your controller-logic within your controller :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.