stats4sd / laravel-shiny-loader
This is my package laravel-shiny-loader
Fund package maintenance!
Statistics for Sustainable Development
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
A Laravel package that lets you embed Shiny apps into your Laravel application with iFrames.
...But surely that doesn't need a package? You're right - you can just use an iFrame and point it at your Shiny app (as long as the app is publicly accessible).
This package does more than that - it provides a way for Laravel to communicate with the Shiny app to:
- pass initialisation data into the app
- ensure that the current user is authenticated before loading the app.
How does it work?
TODO: Update and bring over documentation from : https://github.com/stats4sd/shiny-laravel-auth-example
Installation
You can install the package via composer:
composer require stats4sd/laravel-shiny-loader
Add the following properties to your .env file:
## Path to the directory where shiny apps are served from. For example, if you have shiny apps in /srv/shiny-server/app1 and /srv/shiny-server/app2, set this to /srv/shiny-server SHINY_APP_PATH="/path/to/shiny/app/container"
Use
To enable authentication, you need to set up a route in your Laravel app.
This package provides a Route Macro to make this easy. In your main routes/web.php
(or where-ever you register your routes), add the following:
use Illuminate\Support\Facades\Route; Route::shiny()
If you want to add extra middleware to the route (for example, to ensure the user is authenticated, or has a specific role), you can do so by wrapping the Route::shiny()
call in a middleware group. E.g:
use Illuminate\Support\Facades\Route; Route::middleware(['auth', 'role:admin'])->group(function () { Route::shiny(); });
This will ensure that, even if the user could somehow get to the page that renders the Shiny app, the app will not receive the POST request to confirm the user is authenticated unless your middleware is passed.
The main way to use the package is by adding the ShinyIframe component to your page. It requires a $shinyAppUrl
- the url of the Shiny app you want to embed. You may optionally add $postData
- an array of data to pass to the Shiny app when it loads.
<x-shiny-iframe
:shiny-app-url="$shinyAppUrl"
:post-data="['foo' => 'bar']"
/>
You can use the $postData
to pass any data you want from Laravel to the Shiny app. For example:
- The current user's ID or email address;
- The ID of a resource the user is viewing in Laravel, so the Shiny app can load data related to that resource;
- Any other arbitrary data you want to pass to the Shiny app.
TODO: add refs to documentation on how to setup the Shiny app to receive the postData.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.