cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Response::send() method dosen't print out anything to client

Open guweigang opened this issue 13 years ago • 10 comments

Hi, i tried to use \Phalcon\Http\Response to send response when users didn't have any permission. So i add these to my "Acl.php":

        $obj = $objClient->validate();
        if (!is_object($obj)) {
            // Getting a response instance                                                                       
            $response = $this->_dependencyInjector->get('response');
            // Set status code                                                                                   
            $response->setRawHeader("HTTP/1.1 200 OK");
            // Set the content of the response                                                                   
            $response->setContent(json_encode(\Returns::error($configs->cas->jmpurl, 302, "redirectUrl")));
            // Send response to the client                                                                       
            $response->send();

Like above, it will print out nothing to client. If i force to echo something,

echo $response->send();

i would get fatal errors, cause method send() returns an object(Phalcon\Http\Response), itself???

PHP Catchable fatal error:  Object of class Phalcon\Http\Response could not be converted to string

So, now i have to convert that object to string.

echo @strval($response->send());

Bug or not?

guweigang avatar Jan 15 '13 12:01 guweigang

If you're using Phalcon\Mvc\Application that class implicitly creates a Phalcon\Http\Response which can be override by returning an Phalcon\Http\Response instance from an action handler:

<?php

class FeedController extends Phalcon\Mvc\Controller
{

    public function getAction()
    {
        // Creating a response instance
        $response = new \Phalcon\Http\Request();

        $response->setRawHeader("HTTP/1.1 200 OK");

        //Set the content of the response
        $response->setContent(json_encode("hello"));

        //Return the response
        return $response;
    }

}

ghost avatar Jan 15 '13 18:01 ghost

Hi, @phalcon

I don't want users to visit any controllers/actions. So i do this in dispatcher with an event "beforeDispatch". If someone has no permission, system will return a json-string to tell javascript to jump to somewhere in advance.

$di->set('dispatcher', function() use ($di) {
        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $eventsManager = new \Phalcon\Events\Manager();
        $acl = new \Acl($di);
        $eventsManager->attach('dispatch', $acl);
        $dispatcher->setEventsManager($eventsManager);
        $dispatcher->setDefaultNamespace("Tycoon\Web\Controllers\\");
        return $dispatcher;
    });

and in Acl:

$obj = @$objClient->validate();
if (!is_object($obj)) {
    // Getting a response instance                                                                 
    $response = $this->_dependencyInjector->get('response');
    // Set status code                                                                             
    $response->setRawHeader("HTTP/1.1 200 OK");
    // Set the content of the response                                                             
    $response->setContent(json_encode(\Returns::error($configs->cas->jmpurl, 302, "redirectUrl")));
    // Send response to the client                                                                 
    echo @strval($response->send());
    return false;
}

guweigang avatar Jan 16 '13 03:01 guweigang

if you have an Acl plugin, you could simply echo the json_encode, that output will automatically copied to the Phalcon\Http\Response created by Phalcon\Mvc\Application:

<?php

class Acl extends Phalcon\Mvc\User\Plugin
{
    public function beforeExecuteRoute($event, $dispatcher)
    {
        //...

        $obj = @$objClient->validate();
        if (!is_object($obj)) {         
            // Set status code                                                                             
            $this->response->setRawHeader("HTTP/1.1 200 OK");

            // Set the content of the response                                                             
            echo json_encode(\Returns::error($configs->cas->jmpurl, 302, "redirectUrl"));

            return false;
        }

    }
}

ghost avatar Jan 16 '13 04:01 ghost

@phalcon It has no effect. It will stop dispatcher and not send response to client.

guweigang avatar Jan 16 '13 05:01 guweigang

Ok, try this:

<?php

class Acl extends Phalcon\Mvc\User\Plugin
{
    public function beforeExecuteRoute($event, $dispatcher)
    {
        //...

        $obj = @$objClient->validate();
        if (!is_object($obj)) {

            //Disable auto-rendering mode
            $this->view->disable();

            // Set status code                                                                             
            $this->response->setRawHeader("HTTP/1.1 200 OK");

            // Set the content of the response                                                             
            echo json_encode(\Returns::error($configs->cas->jmpurl, 302, "redirectUrl"));

            return false;
        }

    }
}

ghost avatar Jan 16 '13 06:01 ghost

@phalcon thank u very much.

have tried it... the same problem, nothing was printed out. By the way i use "beforeDispatch" event here.

'Cause i have 4 modules there, and just this module needs permission.

guweigang avatar Jan 16 '13 06:01 guweigang

This one works for me. Do you terminate execution after sending your response? Otherwise you application may try to re-send it and then fails somewhere.

use Phalcon\Http\Response as HttpResponse;

$response = new HttpResponse();
$response->setStatusCode(500, 'Internal Server Error');

/**
 * Send back generic "server error"
 */

if (API_SERVICE) {
    $response->setContentType(JsonResponse::CONTENT_TYPE)
        ->setContent( JsonResponse::build(array('error' => JsonResponse::ERROR_INTERNAL)) );
} else {
    $response->setContentType('text/html')
        ->setContent( file_get_contents(__DIR__ . '/error.html') );
}

$response->send();
exit;

roman-kulish avatar Jan 28 '13 04:01 roman-kulish

@roman-kulish you mean, i should exit() explicitly?

But, if you use system-wide response service, and that response was sent in advanced, the system should be terminated execution. Do you think so?

guweigang avatar Jan 28 '13 08:01 guweigang

It won't terminate execution itself. It will give you a chance to finalize your tasks, close connections, free resources etc. You should explicitly exit if you have nothing more to do. You try first, if it works it will mean that failure happens somewhere else.

roman-kulish avatar Jan 28 '13 10:01 roman-kulish

I'm closing this, please move this issue to the forum or the mailing list if you still have problems, thanks

ghost avatar Feb 04 '13 08:02 ghost