C
C
ChemAli2013-08-12 08:46:19
PHP
ChemAli, 2013-08-12 08:46:19

How to implement a route like /method?par1=val1&...&parn=valn in Slim Framework?


/method/val1/val2/.../valn
If I understood correctly from the documentation, the view routes are welcomed by the framework
/method?par1=val1&...&parn=valn
.

$app->get('/method', 'callback_function');

function callback_function() {
    GLOBAL $app;
    $par1 = $_GET['par1'];
    $par2 = $_GET['par2'];
}

But it seems crooked to me because of the need to declare a global application variable inside the function. In addition, I need to change the "content type" http header for the response, and to do this, I need to change the properties of the $app instance.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
ragimovich, 2013-08-12
@ChemAli

There is nothing strange or scary about the declaration of the global $app, as far as I know. There is no other way to get to it anyway.
If you have fresh PHP, then you can do something like this

$app->get('/method', function() use ($app) {
    $app->request->params('par1');
});

As for the Content-Type, this is decided through $app->response and this is also normal.

J
joger, 2013-08-12
@joger

$app can also be accessed like this:
$app = \Slim\Slim::getInstance();
docs.slimframework.com/#Application-Names-and-Scopes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question