Hi i am learning dynamic urls in php for creating custom router
i set the routes using this method
$router->get("/home",function(){
echo "this is home";
});
I didnt thought before that the first parameter(my router path) of get() could also be written
as /profile/{user}/id/{id}
I tried using regex for path in get() and use preg match to return value
but not happy with output as it requires regex every time i set new route
i would like to know how i would simply set the route like
/profile/{user}/id/{id}
or /profile/{var : user}/id/{int : id}
and and get output like
if route-pattern matches return an array with key value where key is user and value is from string and so on
defined routes : requested routes : output
/home form action : /home o/p = array()
/home/{page} form action : /home/about o/p = array(page=>'about')
/profile/{name}/id/{id} form action : /profile/stackuser/id/200 o/p = array(name=>stackuser,id=>200)
My Answer:
>match('/user/(\d+)', function($id) {
echo "User ID: " . $id;
});
>match('/post/(\d+)', function($id) {
echo "Post ID: " . $id;
});
>match('/category/(\w+)', function($category) {
echo "Category: " . $category;
});
>dispatch($_SERVER['REQUEST_URI']);
Rate this post
4 of 5 based on 7952 votesComments