0

I try to parse my Url to my Router Class. For the begin all is clear i get my Url splitting it into a Array but my Problem at the moment is that i need to add my Routes first like $router->add("user/show/123"); or $router->add("article/2584/edit"); pushing them into a Array and request when its in Array then load Page. But i dont find a way to solve the Parameters when i add them to the Routes. How can i make it like $route->add("article/WHATEVER/edit"); so its loading the Page whatever the number is? I guess with RegEx but iam not really good at it or understanding it. And have no Idea at the moment how to do it i already googled and searched but all i find are finished classes which i dont want to use cause i want to understand how it works at least.

I would be happy for some help some links and some Code Snippets which will give me a new Point of View and a way to go on.

4
  • Who did code routing mechanism? Commented Mar 18, 2017 at 15:53
  • Me i tried different scripts and experimented but then i realized i need a Whitelist or something xD Commented Mar 18, 2017 at 16:15
  • Well article/\d+/edit would be any number in the middle, what does the add do though? Does that take regex? Commented Mar 18, 2017 at 20:29
  • No it only adds the parameter to a array idk at the moment what to do or better sayed how to do the next steps with it. I know what to do but how. I dont really know the regex pattern stuff or how to write the patterns and there is no good tutorial or something out there. Commented Mar 18, 2017 at 23:36

1 Answer 1

1

I did mine in a slightly different way. I take URI's in the following form:

/controller/action/param1/param2/param3/etc.

I route this to index.php?q= using .htaccess, so the entire string becomes accessible by invoking $_GET['q']. I split the string into pieces by using explode with / as a delimiter and the first and the second elements are always the controller and action. Everything else is one string (regardless of parameters) and gets forwarded to the relevant function (action) using a callback. In my function (eg. view) I have $param as an argument, and $param is a string containing /param1/param2/param3/etc.

Within the function, I further split this string into individual parameters, assigning them to regular named variables as needed.

$param = explode('/', $param);
$id = $param[0];
$sortBy = $param[1];
$sortOrder = $param[2];
etc.

Obviously, I check the parameters for validity and use default values if they're empty. The only disadvantage I see is that you MUST provide all 2, 3, 4 (however many you have) parameters you have, even if you want to change only one. In my above example, where I have 3 params, if I only want to change sortOrder, my URI would look something like:

/controller/action/0/0/ASC

Using PHP, I detect that 0 is not a valid parameter for that action and assign it a default value from my settings or other source. The great advantage is simplicity, but also the unlimited number of params you can use for each module. You don't have to "add routes" as these are processed within the controller, not the route file.

PS: I know this is not best practice and it's a very rudimentar approach, but it works. Depending on how far you want to take this, this option might not work for you.

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

2 Comments

Thanks for your answer @Chris , does your suggestion require the use of any framework or this can be done with just core PHP coding? And can you please provide a working sample project. Will be very grateful. Thanks
It uses core PHP only, but at this stage, I would recommend using a framework such as laravel or cakephp. I’m afraid I switched to Python, where I use flask, so I don’t have a worked out example, but there is a lot on routing in PHP using frameworks.

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.