1

I took the suggested solution from PHP: Good solution for MVC routing function to parse requested views? and adujsted it to our internal guidelines (Thanks again Supericy).

I have now following code:

public function parseRequestedView($url) {

    $this->view_requested = explode('/', trim($url, '/'));

    // Format: [0] viewpoint, [1] child, [2] action (default: show), [3] reference
    $this->view_map = array(
        $this->parseViewMapArrayKey('device/:reference:/configurations') => array(0,  2, -1,  1),
        $this->parseViewMapArrayKey('device/:reference:')                => array(0, -1, -1,  1)
    );

    foreach ($this->view_map as $this->view_map_transitory => $this->view_map_indices) {

        if (preg_match($this->view_map_transitory, $url)) {

            foreach ($this->view_map_indices as $this->view_index) {

                $this->view_resources[] = $this->view_index > -1 ? $this->view_requested[$this->view_index] : null;

            }

            return $this->view_resources;

        }

    }

    return false;

}

public function parseViewMapArrayKey($key) {

    return '#'.str_replace([":reference:", ":string:"], ["\d+", ".+"], $key).'#';

}

It is working fine, except one small "problem":

When I switch the keys "device/:reference:" and "device/:reference:/configurations" and then invoke the view for device/:reference:/configurations, I only get the result for device/:reference.

For example, http://ww.foo.com/device/123456/configurations will output:

Array
(
    [0] => device
    [1] => 
    [2] => 
    [3] => 123456
)

Which is the result for http://ww.foo.com/device/123456. When I change the keys back to their original order, everything is like it should be:

Array
(
    [0] => device
    [1] => configurations
    [2] => 
    [3] => 123456
)

How can I reverse the search order or make the function output the right result when I switch the keys?

I found this PHP Reverse Preg_match. But it is about a negative preg_match as far as I can tell. Or I am wrong?

Thanks in advance.

Community
  • 1
  • 1
  • 1
    The problem is that you return from the function the first time that one of the keys from `$$this->view_map` matches. So you need to enter them in priority order. Why are you switching the keys if you don't want to change the preference? Is there a way for the function to figure out the priority automatically? Then use a sort function to reorder it in accordance with this. – Barmar Dec 25 '12 at 12:51
  • Thanks for the answer. It is not really a problem, just was wondering if I did something wrong with the implementation or it worked as designed since I am very new to regex. –  Dec 25 '12 at 14:17
  • I don't really understand your application, so it's hard to tell. Both regexes match the URL, is that supposed to happen? Since they both match, the order matters because it uses the first one that matches. The longer regex is more specific because it also checks for the word after `:reference:`, so you should probably put it first. – Barmar Dec 25 '12 at 14:24
  • Many thanks again. Since the whole thing is time critial, I will add the "problem" to my ToDo list and just leave it for now. But first I will study some material on regular expressions. –  Dec 26 '12 at 13:13
  • Maybe you just need to add a `$` at the end of the regular expression, so that `'device/:reference:'` won't match the URL `http://ww.foo.com/device/123456/configurations`. As I said, I don't understand what the application is doing, so I can't tell if that's appropriate for your needs. – Barmar Dec 26 '12 at 13:44

1 Answers1

0

OK maybe this is a dumb answer but perhaps you could make sure that your regex starts with ^ and ends with $?

For instance

public function parseViewMapArrayKey($key) {

    return '#^'.str_replace([":reference:", ":string:"], ["\d+", ".+"], $key).'$#';

}
alexg
  • 3,015
  • 3
  • 23
  • 36