PHP code example of webiny / http

1. Go to this page and download the library: Download webiny/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webiny / http example snippets


    class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // access `Request` instance
            $this->httpRequest();

            // access `Cookie` instance
            $this->httpCookie();

            // access `Session` instance
            $this->httpSession();

            // create new `Response` instance
            $this->httpResponse('output content');
             
            // redirect
            $this->httpRedirect('http://youtube.com/');
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
           // get request method
            $this->httpRequest()->server()->requestMethod(); // "GET"
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // get 'name' param from current query string
            $this->httpRequest()->query('name');

            // get 'color' param from $_POST, and if color is not defined, return 'blue'
            $this->httpRequest()->post('color', 'blue');
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // get 'name' from payload
            $this->httpRequest()->payload('name');
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // get the uploaded file
            $file = $this->httpRequest()->files('avatar');

            // move it to desired destination
            $file->store('/var/tmp');
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // save into session
            $this->httpSession()->save('my_key', 'some value');

            // read from session
            $this->httpSession()->get('my_key');
        }
    }

class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // save cookie
            $this->httpCookie()->save('my_cookie', 'some value');

            // read cookie
            $this->httpCookie()->get('my_key');
        }
    }


    // using the trait
    class MyClass{
        use \Webiny\Component\Http\HttpTrait;

        function myFunction(){
            // create and sent the output
            $this->httpResponse('Hello World!')->send();
        }
    }

    // using constructor
    $response = new Response('Hello World!');
    $response->send();

    // using static method
    $response = Response::create('Hello World!');
    $response->send();

    $response = new Response('Hello World!');
    $cacheControl = $response->cacheControl();

    // using constructor
    $jsonResponse = new Response\JsonResponse($someArrayOrObject);
    $jsonResponse->send(); // send output to browser

    // short-hand
    Response\JsonResponse::sendJson($someArrayOrObject)); // output is automatically sent