2014-09-20 104 views
4

我正在使用Sliex框架。当我使用\Silex\Application::redirect方法时,我遇到了重定向问题。我发现,当我试图通过http-headers重定向时,而不是symfony“发送”响应似乎称为__toString方法。Symfony响应对象如何设置http标头?

这是我的卷曲输出:

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 

我不明白为什么,但它呼应的HTTP报头。

UPDATE

我的脚本是这样的:

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

路由脚本:

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

登录动作有没有逻辑,它只是呈现一根树枝模板。

+0

你的php脚本是怎样的? – 2014-09-20 08:28:06

+0

您可以添加路由指令和登录控制器吗? – Fractaliste 2014-09-24 11:01:07

回答

1

调用$ APP->重定向()返回直接取自Symfony Response Object Documentation

要设置的Symfony响应对象标题使用以下的302

默认状态的新RedirectResponse对象headers->set方法。

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; 
    } 
} 

一旦方法名称中的拼写错误得到纠正(控制器更改为控制器),您的代码就可以正常工作。

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');