PHP code example of webstronauts / unpoly

1. Go to this page and download the library: Download webstronauts/unpoly 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/ */

    

webstronauts / unpoly example snippets


use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webstronauts\Unpoly\Unpoly;

// ...

$unpoly = new Unpoly();
$unpoly->decorateResponse($request, $response);

use Webstronauts\Unpoly\StackUnpoly;
use Webstronauts\Unpoly\Unpoly;

// ...

$app = new StackUnpoly($app, new Unpoly());



namespace App\Http\Middleware;

use Closure;
use Webstronauts\Unpoly\Unpoly as UnpolyMiddleware;

class Unpoly
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        (new UnpolyMiddleware)->decorateResponse($request, $response);

        return $response;
    }
}



// Within App\Http\Kernel class...

protected $routeMiddleware = [
    // ...
    'unpoly' => \App\Http\Middleware\Unpoly::class,
];



// Within App\Exceptions\Handler class...

protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
    if ($e->response) {
        return $e->response;
    }

    return $request->expectsJson() && ! $request->hasHeader('X-Up-Target')
        ? $this->invalidJson($request, $e)
        : $this->invalid($request, $e);
}

namespace App\Http;

use Illuminate\Http\Request as BaseRequest;

class Request extends BaseRequest
{
    public function expectsJson()
    {
        if ($this->hasHeader('X-Up-Target')) {
            return false;
        }

        return parent::expectsJson();
    }
}

// From...
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

// To...
$response = $kernel->handle(
    $request = App\Http\Request::capture()
);