Routing


Basic Routing

The most basic Shadow routes accept a URI and a Closure, providing a very simple and expressive method of defining routes:

$router->get('foo', function () {
    return 'Hello World';
});

Available Router Methods

Shadow supports GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD, AJAX and ANY request methods.

# GET Request
$router->get('/get-request', function() {
    echo 'Hello World.';
});

# POST Request
$router->post('/post-request', function() {
    echo 'Hello World.';
});

# PUT Request
$router->put('/put-request', function() {
    echo 'Hello World.';
});

# DELETE Request
$router->delete('/delete-request', function() {
    echo 'Hello World.';
});

# AJAX Request
$router->ajax('/ajax-request', function() {
    echo 'Hello World.';
});

# AJAXP Request (Post & Ajax)
$router->ajaxp('/ajaxp-request', function() {
    echo 'Hello World.';
});

# ANY Request (It accepts all requests.)
$router->any('/any-request', function() {
    echo 'Hello World.';
});

# You can define more than one method at one time for a request.
# Example:
$router->add('GET|POST', '/request', function() {
    echo "Hello World. I'm working GET or POST method.";
});

NOTE:
A post value must be sent in an object named "_method" for the Put, Delete, Patch, Options, Head methods.
Example:
curl -X PUT http://localhost:3000/put-request
OR
curl -X POST http://localhost:3000/put-request -d _method=put

$router->put('/put-request', function() {
    echo 'Hello World.';
});

Parameters Pattern

The patterns defined in PHP-Router are:

  • {a} => All chars without '/' char.
  • {d} => Digits.
  • {i} => Digits.
  • {s} => Alphabetic characters.
  • {w} => Alphanumeric characters.
  • {u} => URL format characters for SEO. (Alphanumeric characters, _ and -)
  • {*} => All characters
or
  • :all => All characters
  • :any => All chars without '/' char
  • :id => Digits
  • :number => Digits
  • :string => Alphanumeric characters
  • :slug => URL format characters for SEO. (Alphanumeric characters, _ and -)
Single pattern
# get request -> localhost/hello/{s}
$router->get('/hello/{s}', function($value) {
    echo 'Hello, ' . $value;
});

# OR

$router->get('/hello/:string', function($value) {
    echo 'Hello, ' . $value;
});

# Output:
# /hello/burak ---> Hello, burak
# /hello/php ---> Hello, php
# /hello/ ---> Error!!!
Multi pattern
# get request -> localhost/post/{i}/{u}
$router->get('/post/{i}/{u}', function($id, $slug) {
    echo "Post ID: " . $id . "
"; echo "Post Slug: " . $slug; }); # OR $router->get('/post/:id/:slug', function($id, $slug) { echo "Post ID: " . $id . "
"; echo "Post Slug: " . $slug; }); # Output: # /post/5/hello-world ---> Post ID: 5
Post Slug: hello-world # /post/17/php-router ---> Post ID: 17
Post Slug: php-router # /post/foo/bar ---> Error!!! ({i} params cannot be string.)
optional pattern - add default value
# get request -> localhost/page/{u}
$router->get('/page/{u?}', function($page = 'home')
{
  echo "Page: " . $page;
});

# OR

$router->get('/page/:slug?', function($page = 'home')
{
  echo "Page: " . $page;
});

# Output:
# /page/contact ---> Page: contact
# /page/about-us ---> Page: about-us
# /page ---> Page: home
custom pattern - create own pattern
$router->pattern('{lowercase}', '[a-z]+');
$router->pattern(':uppercase', '[A-Z]+');

$router->get('/demo/{lowercase}', function($value)
{
  echo "Value: " . $value;
});

# Output:
# /demo/burak ---> Value: burak 
# /demo/php ---> Value: php
# /demo/Router ---> Error!!!

$router->get('/demo/:uppercase', function($value)
{
  echo "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. :)

Grouping Routes

$router->group('admin', function($r) {
    $r->get('foo', function(){ echo 'page: admin/foo'; });
    $r->get('bar', function(){ echo 'page: admin/bar'; });
    $r->get('baz/php', function(){ echo 'page: admin/baz/php'; });
    $r->post('login', function(){ echo 'page: admin/login'; });
});

# Created Routes:
/*
- GET /admin/foo
- GET /admin/bar
- GET /admin/baz/php
- POST /admin/login
*/