-
-
Notifications
You must be signed in to change notification settings - Fork 46
4. Parameter Patterns
İzni Burak Demirtaş edited this page May 10, 2021
·
4 revisions
The patterns defined in PHP-Router are:
- :all => All characters
- :any => All chars without '/' char
- :id => Natural numbers (0, 1, 17, 167, 1881, 20956...)
- :number => Real numbers (1, -1.2, 5.5 ...)
- :float => Real numbers
- :bool => Boolean values. (true, false, 1, 0)
- :string => Alphanumeric characters
- :slug => URL format characters for SEO. (Alphanumeric characters, _ and -)
- :uuid => UUID
- :date => Y-m-d format date string
$router->get('/hello/:string', function($value) {
echo "Hello, {$value}";
});
# Output:
# /hello/burak ---> Hello, burak
# /hello/php ---> Hello, php
# /hello/ ---> Error!!!
$router->get('/post/:id/:slug', function($id, $slug) {
echo "Post ID: {$id} <br />";
echo "Post Slug: {$slug}";
});
# Output:
# /post/5/hello-world ---> Post ID: 5 <br />Post Slug: hello-world
# /post/17/php-router ---> Post ID: 17 <br />Post Slug: php-router
# /post/foo/bar ---> Error!!! (:id param must be digits.)
$router->get('/page/:slug?', function($page = 'home') {
return "Page: {$page}";
});
# Output:
# /page/contact ---> Page: contact
# /page/about-us ---> Page: about-us
# /page ---> Page: home
$router->pattern(':lowercase', '[a-z]+');
$router->get('/demo/:lowercase', function($value) {
return "Value: {$value}";
});
# Output:
# /demo/burak ---> Value: burak
# /demo/php ---> Value: php
# /demo/Router ---> Error!!!
To define multiple patterns at once, you can use this method:
$patterns = [':lowcase' => '[a-z]+', ':upcase' => '[A-Z]+'];
$router->pattern($patterns);
Great! That's it. :)