6

I'm using Sliex framework. I had a problem with redirection when I use \Silex\Application::redirectmethod. I found that when I'm trying to redirect by http-headers, instead of symfony "send" the response seemed to call the __toString method.

This is my curl output:

bash-4.2$ curl -v http://127.0.0.1:8082/
* About to connect() to 127.0.0.1 port 8082 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x1ea0970
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1ea0970) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8082 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.32.0
> Host: 127.0.0.1:8082
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 20 Sep 2014 08:02:52 GMT
* Server Apache/2.4.10 (Fedora) PHP/5.5.15 is not blacklisted
< Server: Apache/2.4.10 (Fedora) PHP/5.5.15
< X-Powered-By: PHP/5.5.15
< Set-Cookie: PHPSESSID=*****; path=/
< Content-Length: 116
< Content-Type: text/html; charset=UTF-8
< 
HTTP/1.0 302 Found
Cache-Control: no-cache
Date:          Sat, 20 Sep 2014 08:02:52 GMT
Location:      /login

* Connection #0 to host 127.0.0.1 left intact

I can't understand why but it echo http-headers.

UPDATE

My script is something like this:

<?php class Contorller {
    public action( \Silex\Application $app ){
       return $app->redirect('/login');
    }
}

Routing script:

<?php
$core = new \Silex\Application;
$core->get("/another/action", "\\Controller::action")
$core->match("/login","\\AnotherController::login")->method('POST|GET');

The login action has no logic, and it just renders a twig template.

2
  • How does your php script look like? Commented Sep 20, 2014 at 8:28
  • Can you add your routing instructions and login's controller? Commented Sep 24, 2014 at 11:01

1 Answer 1

5

Calling $app->redirect() returns a new RedirectResponse object with a default status of 302.

The following is taken directly from the Symfony Response Object Documentation

To set headers on the Symfony Response object use the headers->set method.

use \Symfony\Component\HttpFoundation\RedirectResponse;

class Controller {
    public function action( \Silex\Application $app ){

        $response = new RedirectResponse('/login', 302);

        // the headers public attribute is a ResponseHeaderBag
        $response->headers->set('Content-Type', 'text/plain');

        return $response;
    }
}

Your code worked perfectly once the typos in the method names were corrected (Contorller changed to Controller).

class Controller {
    public function action( \Silex\Application $app ){
       return $app->redirect('/login', 302);
    }
}

class AnotherController {
    public function login( \Silex\Application $app ){
       return new Response($app['twig']->render('blank.html.twig'));
    }
}

$app->get("/another/action", "\Controller::action");
$app->match("/login","\AnotherController::login")->method('POST|GET');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.