0

I'm trying to create a Bolt extension to be able to login via a REST endpoint. But I can't capture the values from the request.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

public function initialize()
{
    $this->app->post("/rest/session", array($this, 'rest_login'))
              ->bind('rest_login');
}

public function rest_login(Request $request) {

  // Get credentials
  $username = $request->get('username');
  $password = $request->get('password');

  // Attempt login
  $login = $this->app['users']->login($username, $password);
  $response = $this->app->json(array('login' => $login));
  return $response;
}

If I return after getting $username and $password I can see that these are both NULL, even though they have been sent as POST data - how do I capture these values?

babbaggeii
  • 7,577
  • 20
  • 64
  • 118

1 Answers1

2

This is just a question of understanding the Request/Response process in Silex. The initialize method in your extension is run before the request cycle starts, to access it you need to register a controller which can then setup routes to handle requests. Here's a simple example.

// In Extension.php
public function initialize() {
    $this->app->mount('/', new ExampleController());
}

// In ExampleController.php
class ExampleController implements ControllerProviderInterface
{

    public function connect(Silex\Application $app)
    {
        $ctr = $app['controllers_factory'];
        $ctr->post("/rest/session", array($this, 'rest_login'))
          ->bind('rest_login'); 
    }

    public function rest_login(Request $request) {
        ........
    }
}

That should point you in the right direction.

Ross Riley
  • 826
  • 5
  • 8