Yes, it is possible to create a package with InertiaJS. Here are the steps you can follow:
-
Create a new Laravel package using the
composer create-packagecommand. -
Install InertiaJS and its dependencies using
npm install @inertiajs/inertia @inertiajs/inertia-vue3 vue3. -
Create a new Vue component that extends the
InertiaAppcomponent and register it with Vue.
// resources/js/InertiaApp.js
import { InertiaApp } from '@inertiajs/inertia-vue3'
import { createApp, h } from 'vue'
createApp({
render: () => h(InertiaApp, {
initialPage: JSON.parse(document.getElementById('app').dataset.page),
resolveComponent: name => require(`./Pages/${name}`).default,
})
}).mount('#app')
- Create a new InertiaJS view in your package and use the
@inertiaBlade directive to render it.
// resources/views/my-package-view.blade.php
@extends('my-package::layout')
@section('content')
<div>
<h1>My Package View</h1>
<p>Welcome to my package view!</p>
</div>
@endsection
@push('scripts')
@vite('resources/js/app.js')
@endpush
- Create a new route in your package's
routes/web.phpfile that returns the InertiaJS view.
// routes/web.php
use MyPackage\Http\Controllers\MyPackageController;
Route::get('/my-package', [MyPackageController::class, 'index'])->name('my-package.index');
- Create a new controller in your package that returns the InertiaJS view.
// src/Http/Controllers/MyPackageController.php
namespace MyPackage\Http\Controllers;
use Inertia\Inertia;
class MyPackageController
{
public function index()
{
return Inertia::render('my-package-view');
}
}
-
Publish your package's assets using the
php artisan vendor:publishcommand. -
Use your package in a Laravel application by installing it via Composer and registering its service provider.
// config/app.php
'providers' => [
// ...
MyPackage\MyPackageServiceProvider::class,
],
- Use the
routehelper function to generate a URL to your package's InertiaJS view.
// resources/views/welcome.blade.php
<inertia-link href="{{ route('my-package.index') }}">My Package View</inertia-link>
That's it! You should now be able to navigate to your package's InertiaJS view by clicking the link on your Laravel application's homepage.