Get url parameter in GET request using REST

Asked

Viewed 160 times

0

Hello guys I have a question. When I make the request:

GET /Withdraw/30

Since the value "30" is not linked to a key, then you cannot use the $_GET['chave'].

The way I thought was to do this:

$valor = end(explode("/", $_SERVER['REQUEST_URI']));

It’s not very pretty, but it works.

Is there a better way to do this without using a Framework? In Cakephp I do it well, but without it I’ve complicated the field.

1 answer

1

PHP does not work natively with this URL format and with so many Packages so they perform the function of routing, I don’t see much point in redoing the wheel.

What you can do is use some component that does the routing of your application. How about using a microframework already supported by routing, like Silex or Lumen?

Simple example with Silex:

<?php

require_once __DIR__.'/../vendor/autoload.php'; 

$app = new Silex\Application(); 

$app->get('/hello/{name}', function($name) use($app) { 
    return 'Hello '.$app->escape($name); 
}); 

$app->run(); 

Example with Lumen (filing cabinet app/Http/routes.php)

$app->get('/', function () {
    return 'Hello World';
});

$app->post('foo/bar', function () {
    return 'Hello World';
});
  • Great guy, I don’t know if it’s worth it for the moment I use it, but I liked the idea and when I need something more robust I know it exists. The application I am making is simple and only has GET request, with a single URL, is a practical source for a job. Thanks for the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.