0
votes

I would like to write a router that is parsing an URL such as I am giving it the rules and after that I would like to have an array that is name based.

======

EDITED:

So I did rewrite the code a little bit the new code can be found here: http://pastebin.com/Ver601FF

The outcome can be seen here: http://img824.imageshack.us/i/phpprob.png/

What I now dont get is the way I could match the url values according to the regex within an array that has the indexes as the second route param has.

========================EDIT STOP================== At the end I would like to have an array:

$request['controller']
$request['action']

as the basic $request params, as I need them all the time- therefore this has to be my standard / default route.

So lets say the $pattern[2] is happening, when I finish with this horrible regex I would like to have this:

$request['controller']
$request['action'] = show
$request['id']
$request['name']

obviously everything has to be filled accordingly depending on the requested url...

It dont know if it is because of its late or not, but I cant figure out, how I have to match those things.

Do I need to split the url and after I was checking if it matches a route? and then work with the values that they go to each others.

Another problem where I dont know a solution to is the fact, that an url such as

"/information/show/some/other/stuff"

has to give me back something like

$request['controller']  = information
$request['action'] = show
$request['params'] = array(some, other, stuff)

I appreciate anykind of help and hope that somebody can help me out of this trouble :(

Thank you very much in advance.

2

2 Answers

1
votes
$regex  .= "[a-zA-Z]\/";

should be

$regex  .= "[a-zA-Z]+\/";

The first matches one letter. The second matches one or more.

Furthermore, your slashes are at the wrong spot ([]/[]/ vs /[]/[]) and your match is not anchored.

...
$regex .= "\/[a-zA-Z]+";
...
$regex .= "\/" . $route;
...
print_r(preg_grep("/^" . $patt . "\z/i", $url));
...
0
votes

I found out the answer to my question on my own - I hope it helps somebody out there. But I might even publish it somewhere after a little freshup.

Check out the solution: http://pastebin.com/FAbhneMZ

I am going to rewrite it now and produce a proper class out of it, if there is anybody who is thinking that the logic in this little router can be improved please let me know!

All the best guys.

EDIT changed the link because that version was still buggy and not good and this is the final solution I am using and after 2 days more testing I think its just working the way I needed it. Hope it helps somebody else!