Download the PHP package crenspire/yii2-inertia without Composer
On this page you can find all versions of the php package crenspire/yii2-inertia. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-inertia
Yii2 Inertia.js Adapter
An Inertia.js adapter for Yii2 framework, providing a seamless bridge between your Yii2 backend and modern JavaScript frontend frameworks (React, Vue, Svelte).
Features
- ๐ Simple API: Match the developer experience of
inertia-laravel - ๐ฆ Shared Props: Share data across all Inertia responses
- ๐ Partial Reloads: Support for partial page updates
- ๐ฏ Asset Versioning: Automatic version management for cache busting
- ๐งช Well Tested: Comprehensive unit and integration tests
- ๐ Full Documentation: Complete usage examples and guides
Installation
Install via Composer:
Quick Start
1. Configure Your Application
In your config/web.php, register the Inertia view renderer:
2. Create Root View Template
Create a root view template at views/layouts/inertia.php:
3. Use in Controllers
4. Setup Frontend
Install Inertia.js and your frontend framework:
Create src/main.jsx:
API Reference
Inertia::render()
Render an Inertia page:
Inertia::share()
Share data with all Inertia responses:
Inertia::version()
Set or get the asset version:
Inertia::location()
Create an Inertia redirect response:
Global Helper
You can also use the global inertia() helper function:
Partial Reloads
Inertia supports partial reloads for better performance. The client can request only specific props:
Redirects
For POST/PUT/PATCH/DELETE requests, Inertia handles redirects automatically:
The Inertia::location() method automatically detects the request type:
- Inertia requests: Returns HTTP 409 with
X-Inertia-Locationheader - Regular requests: Returns HTTP 302 with
Locationheader
Version Management
Inertia.js uses version checking to ensure the frontend and backend stay in sync. When the client's version doesn't match the server's version, a full page reload is triggered.
Automatic Version Detection
By default, the version is automatically detected from your manifest.json file:
Custom Version
You can set a custom version:
Version Mismatch Handling
When a client sends an X-Inertia-Version header that doesn't match the current version, the adapter automatically returns a location redirect (409 status) to trigger a full page reload. This ensures users always have the latest assets.
Configuration
Root View Path
You can configure the root view path:
Bootstrap/Initialization
For shared props that should be available on every page, you can set them in your application bootstrap or a common controller:
Troubleshooting
Version Mismatch Issues
If you're experiencing frequent full page reloads, check:
- Your version callback is returning a stable value
- The
manifest.jsonfile exists and is accessible - File permissions allow reading the manifest file
Redirect Not Working
If redirects aren't working as expected:
- Ensure you're using
Inertia::location()instead of Yii'sredirect() - Check that the request has the
X-Inertiaheader for Inertia requests - Verify the response status code (409 for Inertia, 302 for regular)
Root View Not Found
If you get "Root view file not found" errors:
- Verify the path in
Inertia::setRootView()is correct - Check that the view file exists and is readable
- Ensure Yii aliases are properly configured
Running the Example
The repository includes a complete example application. To run it:
Visit http://localhost:8000 in your browser.
Testing
Run the test suite:
Requirements
- PHP ^8.1
- Yii2 ~2.0.50
License
MIT License. See LICENSE file for details.
Contributing
Please see CONTRIBUTING.md for details.
Changelog
See CHANGELOG.md for a list of changes.