PHP code example of putyourlightson / laravel-datastar
1. Go to this page and download the library: Download putyourlightson/laravel-datastar library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
putyourlightson / laravel-datastar example snippets
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\StreamedResponse;
class MyController extends Controller
{
public function index(): StreamedResponse
{
$signals = sse()->readSignals();
$enabled = $signals['enabled'] ?? false;
sse()->patchSignals(['enabled' => !$enabled]);
sse()->patchElements('
<span id="button-text">' . ($enabled ? 'Enable' : 'Disable') . '</span>
');
return sse()->getEventStream();
}
public function view(): StreamedResponse
{
sse()->renderView('path.to.view');
return sse()->getEventStream();
}
}