Fork me on GitHub

Application - Response

#Response

#Feature

After receiving and interpreting a request message, a server responds with an HTTP response message. the CL framework support 5 ways responses. It is easily to do.

We MUST put your response PHP code in your Controllers(example. MemberController.php, AuthController.php etc.)

#Support Response Type

  • JSON
  • XML
  • HTML (Html Template is more complicated, Click `Here` more information )
  • Redirect

#How To Use

#JSON

However, if you are not using Homestead, you will need to make sure your server meets the following requirements:

PHP Code


$this->addViewData("testStringData", "testData");
$this->addViewData("testKeyArrayData", array("a","b"));
$this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));

return Response::create()->setResponseType("json")->setBodyData($this->getViewData())->run();

Response


{
    "testStringData": "testData",
    "testKeyArrayData": [
        "a",
        "b"
    ],
    "testKeyValueArrayData": {
        "key1": "value1",
        "key2": "value2"
    }
}

#XML

However, if you are not using Homestead, you will need to make sure your server meets the following requirements:

PHP Code


$this->addViewData("testStringData", "testData");
$this->addViewData("testKeyArrayData", array("a","b"));
$this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));

return Response::create()->setResponseType("xml")->setBodyData($this->getViewData())->run();

Response


<code>200</code>
<message>Success</message>
<body>
    <testStringData>testData</testStringData>
    <testKeyArrayData>
        <num_0>a</num_0>
        <num_1>b</num_1>
    </testKeyArrayData>
    <testKeyValueArrayData>
        <key1>value1</key1>
        <key2>value2</key2>
    </testKeyValueArrayData>
</body>

#HTML

However, if you are not using Homestead, you will need to make sure your server meets the following requirements:

PHP Code


$this->addViewData("testStringData", "testData");
$this->addViewData("testKeyArrayData", array("a","b"));
$this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));
$this->setViewLayout(array('LAYOUT' => 'testhtml.html'));

return Response::create()->setResponseType("html")->setViewLayout($this->getViewLayout())->setBodyData($this->getViewData())->run();

Response


No body text just view as html tags

#REDIRECT

However, if you are not using Homestead, you will need to make sure your server meets the following requirements:

PHP Code


return Response::create()->setResponseType("redirect")->setRedirectUrl("https://www.google.com")->run();

Response


No Response View, Just move to url like google.com

#Full Example

#index.php


<?php

require_once("/home/projects/my_project/autoload.php");
require_once("/home/projects/my_project/controller.php");


class TestRoute extends \CafeLatte\Core\BaseRoute implements \CafeLatte\Interfaces\RouterInterface
{
    public function routing()
    {

        $this->router->get("ResponseJson", function () {
            $this->result = (new TestController($this->request, $this->log))->getJsonResponse();
        });
        $this->router->get("ResponseXml", function () {
            $this->result = (new TestController($this->request, $this->log))->getXmlResponse();
        });
        $this->router->get("ResponseHtml", function () {
            $this->result = (new TestController($this->request, $this->log))->getHtmlResponse();
        });
        $this->router->get("ResponseText", function () {
            $this->result = (new TestController($this->request, $this->log))->getTextResponse();
        });
        $this->router->get("ResponseRedirect", function () {
            $this->result = (new TestController($this->request, $this->log))->getRedirectResponse();
        });


        //DO NOT REMOVE
        $this->router->run();
    }

}

$framework = new TestRoute();
$framework->execute();

# Example Controller Code(MyController.php)


use CafeLatte\Core\Controller;
use CafeLatte\Interfaces\ControllerInterface;
use CafeLatte\Interfaces\HttpRequestInterface;
use CafeLatte\Interfaces\LoggerInterface;

class MyController extends Controller implements ControllerInterface
{

    public function __construct(HttpRequestInterface $request, LoggerInterface $log)
    {
        parent::__construct($request, $log);
    }

    /**
     * sample json response code in controller
     */
    public function getJsonResponse(){
        $this->addViewData("testStringData", "testData");
        $this->addViewData("testKeyArrayData", array("a","b"));
        $this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));

        return Response::create()->setResponseType("json")->setBodyData($this->getViewData())->run();
    }

    /**
     * sample xml response code in controller
     */
    public function getXmlResponse(){
        $this->addViewData("testStringData", "testData");
        $this->addViewData("testKeyArrayData", array("a","b"));
        $this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));

        return Response::create()->setResponseType("xml")->setBodyData($this->getViewData())->run();
    }

    /**
     * sample html response code in controller
     */
    public function getHtmlResponse(){
        $this->addViewData("testStringData", "testData");
        $this->addViewData("testKeyArrayData", array("a","b"));
        $this->addViewData("testKeyValueArrayData", array("key1" => "value1","key2" => "value2"));
        $this->setViewLayout(array('LAYOUT' => 'index.html'));

        return Response::create()->setResponseType("html")->setViewLayout($this->getViewLayout())->setBodyData($this->getViewData())->run();
    }

    /**
     * sample text response code in controller
     */
    public function getTextResponse(){
        return Response::create()->setResponseType("text")->setBodyData("test Message")->run();
    }

    /**
     * sample redirect response code in controller
     */
    public function getRedirectResponse(){
         return Response::create()->setResponseType("redirect")->setRedirectUrl("https://www.facebook.com")->run();
    }

}

#Tutorial

To get more information, move to tutorial page, Click `Here`