-
-
Notifications
You must be signed in to change notification settings - Fork 46
3. Methods
PHP Router supports GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD, AJAX Requests and ANY request methods.
# GET Request
$router->get('/get-request', function() {
return 'Hello World.';
});
# POST Request
$router->post('/post-request', function() {
return 'Hello World.';
});
If you want to use Request and Response instance in Router, you can bind them to your Route callback. For example:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
# POST Request
$router->post('/post-request', function(Request $request, Response $response) {
return $response->setContent($request->getMethod());
});
You can check Symfony HTTP Foundation to get more information about Request and Response instance.
Available HTTP Methods List for PHP-Router:
- ANY => All request methods
- GET => Get method
- POST => Post method
- PUT => Put method
- DELETE => Delete method
- HEAD => Head method
- OPTIONS => Options method
- PATCH => Patch method
-
AJAX => Request must be
XmlHttpRequest
[DEPRECATED - Use XGET] -
XGET => Request must be
XmlHttpRequest
-
XPOST => Request must be
XmlHttpRequest
and Post method -
XPUT => Request must be
XmlHttpRequest
and Put method -
XDELETE => Request must be
XmlHttpRequest
and Delete method -
XPATCH => Request must be
XmlHttpRequest
and Patch method
Other examples:
# XPOST Request (Ajax & Post)
$router->xpost('/ajaxp-request', function() {
return 'Hello World.';
});
# ANY Request (It accepts all requests.)
$router->any('/any-request', function() {
return 'Hello World.';
});
Also, you can define more than one method at one time for a request.
Example:
$router->add('GET|POST', '/request', function() {
return "Hello World. I'm working GET or POST method.";
});
If you want to get information about Routing for Controllers, you can check Controllers Usage Page.
Finally, Response
of the PHP-Router works three way:
1- You can use string
definition directly. This string will be added to Response
instance automatically.
$router->get('/', function() {
return 'Hello World.';
});
# "Hello World" string will be added into Response instance of Symfony.
2- You can use Response
instance directly. In this case, send()
method will be triggered by PHP-Router automatically. So, don't use ->send()
method in your callback.
use Symfony\Component\HttpFoundation\Response;
$router->get('/', function(Response $response) {
return $response->setContent('Hello World');
});
3- You can use array
directly. In this case, the array
will be converted to json
string and sent a response with application/json
headers via Response instance of Symfony.
$router->get('/', function() {
return ['Hello', 'World'];
});
You can check Symfony HTTP Foundation to get more information about Request and Response instance.
NOTE:
A post value must be sent in an object named "_method" for the Put, Delete, Patch 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() {
return 'Hello World.';
});
You can define some options for the Routes. For example; you can specify a beforeMiddleware or afterMiddleware. Also, you can even specify a name for the Route. This options will be more in the future.
To use options, you should add an array
which contain your options to third parameter of the method.
Let's check an example:
$router->get('/test', function() {
return 'Hello World.';
}, ['name' => 'test']);
Also, you can define middleware for the Route. There is an other page for this. You can check Middleware usage.