Including Assets
Laravel Driver.js needs the Driver.js CSS and JavaScript to be loaded on your page before it can render tours. The package supports multiple ways to include these assets.
CDN Method (Default)
The simplest method — just add the @driverjsAssets directive to your layout:
<head>
@driverjsAssets
</head>This outputs the Driver.js CSS link and JavaScript script tag from the jsDelivr CDN:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/driver.js@1.4.0/dist/driver.css">
<script src="https://cdn.jsdelivr.net/npm/driver.js@1.4.0/dist/driver.js.iife.js"></script>Customizing the CDN Version
Set the version in your .env file:
DRIVERJS_VERSION=1.4.0Or use latest to always get the newest version:
DRIVERJS_VERSION=latestCustom CDN URLs
If you're using a custom CDN or self-hosting the files, you can override the URLs:
DRIVERJS_CDN_JS_URL=https://your-cdn.com/driver.js
DRIVERJS_CDN_CSS_URL=https://your-cdn.com/driver.cssNPM Method
If you prefer to bundle Driver.js with your Vite build, set the asset source to npm:
// config/driverjs.php
'asset_source' => 'npm',Or via .env:
DRIVERJS_ASSET_SOURCE=npmThen install the package:
npm install driver.jsImport it in your resources/js/app.js:
import { driver } from "driver.js";
import "driver.js/dist/driver.css";
// Make the driver available globally for Laravel Driver.js
window.driver = { js: { driver } };When using the NPM method, the @driverjsAssets directive outputs nothing and renderAssetLinks() returns an empty string (since you're bundling the assets yourself).
Custom Method
If you want full control over asset loading, set the asset source to custom:
// config/driverjs.php
'asset_source' => 'custom',The package will not inject any scripts or stylesheets. You're responsible for including the Driver.js library on your own.
Rendering Assets Manually
If you need to render the asset links outside of a Blade directive:
$assetLinks = app('driverjs')->renderAssetLinks();This returns the HTML string containing the <link> and <script> tags when asset_source is cdn. For npm and custom, it returns an empty string.
Important Notes
- The CDN method loads Driver.js globally as
window.driver.js.driver, which is what the generated JavaScript uses to create driver instances. - When using the NPM method, you must expose the driver on
window.driver.js.driverfor the generated JavaScript to work correctly. - The
render()method automatically includes asset links whenasset_sourceiscdn. UsetoScriptTag()ortoJavaScript()if you want to render only the tour script without asset links.
That's it! You're now equipped to include Driver.js assets using Laravel Driver.js.