blueweb / nette-ajax-history
Adds History API support for Nette Framework 3.2+ (readonly Application).
Requires
- php: >=8.2
- nette/application: ^3.2
- nette/di: ^3.1
- nette/http: ^3.3
- nette/routing: ^3.0
- nette/utils: ^3.2 || ^4.0
README
Adds History API support to nette.ajax.js addon for Nette Framework!
This repo is a fork of vojtech-dobes/history.nette.ajax.js package with additional support for Nette 3.
All credits go to Vojtěch. Thank you!
Compatibility
| Package version | Nette |
|---|---|
^4.0 |
nette/application 3.2+ (readonly Application), nette/http 3.3+, PHP 8.2+ |
^3.0 |
Nette 3.0 – 3.1 |
4.0.0 is a compatibility release for the readonly Application era. Since nette/application 3.2 the Application::$httpRequest property is a promoted readonly property, so the previous reflection-based request swapping no longer works. The redirect re-run now uses the public Application::processRequest() API and is wrapped in the same lifecycle as Application::run() (onStartup / onShutdown, and onError on failure) so it behaves consistently with a normal request.
Note: because the redirect re-run fires
onStartup/onShutdownagain within the same HTTP request, handlers bound to these events must be idempotent (this was already the case with the previousrun()-based implementation).
Installation
- Link
client-side/history.ajax.jsafternette.ajax.js. - Load PHP files with Composer:
blueweb/nette-ajax-history - Register config extension in your configuration:
extensions:
ajaxHistory: Blueweb\NetteAjax\HistoryExtension
Usage
Write your application as normal. All redirects and forwards will be properly handled.
To correctly update UI, use snippets. If you plan to ajaxify whole application, consider adding this snippet to your beforeRender() method in BasePresenter.
if ($this->isAjax()) { $this->invalidateControl('title'); $this->invalidateControl('content'); }
And app/@layout.latte might be upgraded accordingly:
<title n:inner-snippet="title">...</title>
{snippet content}
{include content}
{/snippet}
Disabling
If you want specific links or forms exclude from scope of this addon (not make them update the URL), you can use data-ajax-off feature of nette.ajax.js.
<a class="ajax" data-ajax-off="history">
UI Caching
Extension will automatically cache your UI and restore it on Back and Forward buttons without communication with server. If you wish to call server on every Back and Forward, turn caching off.
$.nette.ext('history').cache = false;
Changelog
4.0.0
- BREAKING: requires
nette/application3.2+ (readonlyApplication). Not compatible with Nette < 3.2. - Replaced reflection-based
Application::$httpRequestswapping with the publicApplication::processRequest()API (the property is nowreadonlyand cannot be mutated). - Redirect re-run is wrapped in the same lifecycle events as
Application::run()(onStartup/onShutdown,onErroron failure). - Fixed
Nette\Http\Requestconstruction fornette/http3.3 (non-nullablearray/stringconstructor arguments). - Declared previously transitive dependencies explicitly:
nette/routing,nette/utils. - Removed the legacy Nette 2.x
Nette\Config\CompilerExtensionclass alias.