search

Home  >  Q&A  >  body text

About the return method in laravel

Why in laravel, return "xxx"; will directly return xxx and output it to the current page. And return "xxxx" in php will not output anything on the page. Are they the same method? How can I use PHP's original return method in Laravel without outputting content on the page?

phpcn_u1582phpcn_u15822750 days ago1161

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-16 16:56:48

    In Laravel, the page is not output just because you return, the returned things need to be sent out through the Response,再通过它的sendmethod
    .

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:56:48

    composer intasll can download half of the Internet

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:56:48

    Looking at the Laravel source code, the answer lies in the SymfonyComponentHttpFoundationResponse class

    The entry file calls a $response->send() method

    public function send()
    {
        $this->sendHeaders();
        $this->sendContent();
    
        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        } elseif ('cli' !== PHP_SAPI) {
            static::closeOutputBuffers(0, true);
        }
    
        return $this;
    }
    

    Then look at the sendContent() method

    public function sendContent()
    {
        echo $this->content;
    
        return $this;
    }
    

    I see here that echo is still used

    reply
    0
  • Cancelreply