jupitern / slim3-skeleton

Slim3 skeleton (http + cli) with some add-ons out of the box
44 stars 12 forks source link

How to get the attributes sent via get #9

Closed jerfeson closed 5 years ago

jerfeson commented 6 years ago

I'm trying something simple, get the parameters via get, but something seems not to be right. Can anyone help me understand?

At this point I have the attribute

$app->any('/admin/{module}/{class}/{method}[/{id}]', function(Request $request, Response $response, $args) use($app) {
    $id = $request->getAttribute('id');
    return $app->resolveRoute('\App\Http\Admin', $args['module'], $args['class'], $args['method'], $args);
});

But not here

class Class  extends Controller
{
   /**
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function index()
    {
        $id = $this->request->getAttribute('id');

    }

}
jupitern commented 6 years ago

Hi, route params and get/post params are two diferent things. example: If you have a route like

$app->any('/welcome/{id}', function(Request $request, Response $response, $args) use($app) {
    return $app->resolveRoute('\App\Http\Site', 'Welcome', 'index', $args);
});

and you call: http://localhost:8080/index.php/welcome/10?x=20

your controller welcome can be like:

<?php

namespace App\Http\Site;
use \App\Http\Controller;

class Welcome extends Controller
{

    public function index($id)
    {
        // get route param id. this gets automatically injected by the framework into the method.
        echo $id . "<br/>";

        // get para id from GET
        echo $this->request->getQueryParam('id') . "<br/>";

       // get all GET params
        debug($this->request->getQueryParams());

        // get all POST params
        debug($this->request->getParsedBody());
    }
}