Dave Stein
Dave Stein

Reputation: 9316

How do I make an internal redirect in Silex / Symfony?

I have users getting auto-generated when they login via social media. I don't get their email so I want to make it that when they land on /whatever/page/after/login they see a screen that's like "Just enter email to continue on!"

I took a look at http://silex.sensiolabs.org/doc/cookbook/sub_requests.html and I'm either misreading or thinking I'd need to do it within a Silex\ControllerProviderInterface. I want this behavior to be for any request. Meanwhile if I make all my providers extend one, I'm not sure of the right way to cut out of a parent's connect without botching everything.

I also tried re-initializing everything similar to the answer here Unable to overwrite pathInfo in a Symfony 2 Request.

Here is the code I'm working with:

$app
->before(function (Request $request) use ($app) {
      $token = $app['security']->getToken();
      $app['user'] = null;

      if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
          $app['user'] = $token->getUser();


          if (!$app['user']->isVerified()) {
            $request->server->set('REQUEST_URI', '/signup');
            $request->initialize($request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->getContent());
          }
      }
    });

Upvotes: 2

Views: 1160

Answers (1)

PetersenDidIt
PetersenDidIt

Reputation: 25620

I believe what you want to do is create a new Request object with the correct values and then tell that app to handle it.

If you don't care about preserving the request params from the original request then you can strip a lot of the extra stuff out.

$app
->before(function (Request $request) use ($app) {
      $token = $app['security']->getToken();
      $app['user'] = null;

      if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
          $app['user'] = $token->getUser();


          if (!$app['user']->isVerified()) {
            $subRequest = Request::create('/signup', 'GET', $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->getContent());
            $subRequest->request = $request->request;
            $subRequest->query = $request->query;
            return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
          }
      }
    });

Upvotes: 1

Related Questions