{"id":18769686,"url":"https://github.com/webstronauts/php-unpoly","last_synced_at":"2025-04-13T07:31:35.660Z","repository":{"id":34925109,"uuid":"190603919","full_name":"webstronauts/php-unpoly","owner":"webstronauts","description":"Stack middleware for handling Javascript Unpoly Framework requests.","archived":false,"fork":false,"pushed_at":"2023-01-19T12:36:26.000Z","size":31,"stargazers_count":22,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-23T01:21:11.868Z","etag":null,"topics":["php","stackphp","unpoly"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webstronauts.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-06T15:19:58.000Z","updated_at":"2024-04-07T11:33:14.000Z","dependencies_parsed_at":"2023-02-11T00:00:39.940Z","dependency_job_id":null,"html_url":"https://github.com/webstronauts/php-unpoly","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webstronauts%2Fphp-unpoly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webstronauts%2Fphp-unpoly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webstronauts%2Fphp-unpoly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webstronauts%2Fphp-unpoly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webstronauts","download_url":"https://codeload.github.com/webstronauts/php-unpoly/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670520,"owners_count":21142896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","stackphp","unpoly"],"created_at":"2024-11-07T19:16:38.292Z","updated_at":"2025-04-13T07:31:35.408Z","avatar_url":"https://github.com/webstronauts.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unpoly\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/webstronauts/unpoly.svg?style=flat-square)](https://packagist.org/packages/webstronauts/unpoly)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/webstronauts/php-unpoly/run-tests.yml?branch=main\u0026style=flat-square)](https://github.com/webstronauts/php-unpoly/actions?query=workflow%3Arun-tests)\n[![StyleCI](https://github.styleci.io/repos/190603919/shield?branch=master)](https://github.styleci.io/repos/190603919)\n[![Total Downloads](https://img.shields.io/packagist/dt/webstronauts/unpoly.svg?style=flat-square)](https://packagist.org/packages/webstronauts/unpoly)\n\nStack middleware for handling [Javascript Unpoly Framework](https://unpoly.com) requests.\n\n\u003ca href=\"https://webstronauts.com/\"\u003e\n    \u003cimg src=\"https://webstronauts.com/badges/sponsored-by-webstronauts.svg\" alt=\"Sponsored by The Webstronauts\" width=\"200\" height=\"65\"\u003e\n\u003c/a\u003e\n\n## Installation\n\nYou can install the package via [Composer](https://getcomposer.org).\n\n```bash\ncomposer require webstronauts/unpoly\n```\n\n## Usage\n\nYou can manually decorate the response with the `Unpoly` object.\n\n```php\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Webstronauts\\Unpoly\\Unpoly;\n\n// ...\n\n$unpoly = new Unpoly();\n$unpoly-\u003edecorateResponse($request, $response);\n```\n\n### Stack Middleware\n\nYou can decorate the response using the supplied [Stack](http://stackphp.com) middleware.\n\n```php\nuse Webstronauts\\Unpoly\\StackUnpoly;\nuse Webstronauts\\Unpoly\\Unpoly;\n\n// ...\n\n$app = new StackUnpoly($app, new Unpoly());\n```\n\n### Laravel\n\nTo use the package with Laravel, you'll have to wrap it around a middleware instance.\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Middleware;\n\nuse Closure;\nuse Webstronauts\\Unpoly\\Unpoly as UnpolyMiddleware;\n\nclass Unpoly\n{\n    /**\n     * Handle an incoming request.\n     *\n     * @param  \\Illuminate\\Http\\Request  $request\n     * @param  \\Closure  $next\n     * @return mixed\n     */\n    public function handle($request, Closure $next)\n    {\n        $response = $next($request);\n\n        (new UnpolyMiddleware)-\u003edecorateResponse($request, $response);\n\n        return $response;\n    }\n}\n```\n\nNow use this middleware as described by the [Laravel documentation](https://laravel.com/docs/master/middleware).\n\n```php\n\u003c?php\n\n// Within App\\Http\\Kernel class...\n\nprotected $routeMiddleware = [\n    // ...\n    'unpoly' =\u003e \\App\\Http\\Middleware\\Unpoly::class,\n];\n```\n\n#### Validation Errors\n\nWhenever a form is submitted through Unpoly, the response is returned as JSON by default. This is because Laravel returns JSON formatted response for any request with the header `X-Requested-With` set to `XMLHttpRequest`. To make sure the application returns an HTML response for any validation errors, overwrite the `convertValidationExceptionToResponse` method in your `App\\Exceptions\\Handler` class.\n\n```php\n\u003c?php\n\n// Within App\\Exceptions\\Handler class...\n\nprotected function convertValidationExceptionToResponse(ValidationException $e, $request)\n{\n    if ($e-\u003eresponse) {\n        return $e-\u003eresponse;\n    }\n\n    return $request-\u003eexpectsJson() \u0026\u0026 ! $request-\u003ehasHeader('X-Up-Target')\n        ? $this-\u003einvalidJson($request, $e)\n        : $this-\u003einvalid($request, $e);\n}\n```\n\n#### Other HTTP Errors\n\nIf your Laravel session expires and a user attempts to navigate or perform an operating on the page using Unpoly, an abrupt JSON error response will be displayed to the user:\n\n```\n{'error': 'Unauthenticated.'}\n```\n\nTo prevent this, create your own `Request` and extend Laravel's built-in `Illuminate\\Http\\Request`, and override the `expectsJson` method:\n\n```php\nnamespace App\\Http;\n\nuse Illuminate\\Http\\Request as BaseRequest;\n\nclass Request extends BaseRequest\n{\n    public function expectsJson()\n    {\n        if ($this-\u003ehasHeader('X-Up-Target')) {\n            return false;\n        }\n\n        return parent::expectsJson();\n    }\n}\n```\n\nThen, navigate to your `public/index.php` file, and update the usage:\n\n\n```php\n// From...\n$response = $kernel-\u003ehandle(\n    $request = Illuminate\\Http\\Request::capture()\n);\n\n// To...\n$response = $kernel-\u003ehandle(\n    $request = App\\Http\\Request::capture()\n);\n```\n\nNow when a user session expires, the `\u003cbody\u003e` of your page will be replaced with your login page, allowing users to sign back in without refreshing the page.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Credits\n\nAs it's just a simple port of Ruby to PHP code. All credits should go to the Unpoly team and their [unpoly](https://github.com/unpoly/unpoly) gem.\n\n- [Robin van der Vleuten](https://github.com/robinvdvleuten)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebstronauts%2Fphp-unpoly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebstronauts%2Fphp-unpoly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebstronauts%2Fphp-unpoly/lists"}