PHP code example of moonshine / twirl
1. Go to this page and download the library: Download moonshine/twirl 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/ */
moonshine / twirl example snippets
use MoonShine\Twirl\Components\Twirl;
Twirl::make(),
use MoonShine\Twirl\Events\TwirlEvent;
TwirlEvent::dispatch(
selector: '.your-selector' . $id,
(string) Badge::make(),
HtmlReloadAction::OUTER_HTML
);
/**
* @see https://github.com/centrifugal/phpcent
*/
declare(strict_types=1);
namespace App\Services;
use Throwable;
use phpcent\Client;
use MoonShine\Twirl\DTO\TwirlData;
use MoonShine\Twirl\Contracts\TwirlBroadcastContract;
final class Centrifugo implements TwirlBroadcastContract
{
public function send(string $channel, TwirlData $twirlData): void
{
try {
$client = new Client(config('app.centrifugo.host'). '/api', config('app.centrifugo.api-key'));
$client->publish($channel, $twirlData->toArray());
} catch (Throwable $e) {
report($e);
}
}
}
$this->app->bind(TwirlBroadcastContract::class, Centrifugo::class);
use MoonShine\Laravel\Http\Controllers\MoonShineController;
use phpcent\Client;
class CentrifugoController extends MoonShineController
{
public function index()
{
$client = new Client(
url: 'http://centrifugo-url:8000/api',
apikey: '...',
secret: '...'
);
return response()->json([
'token' => $client->generateConnectionToken($this->auth()->user()->id, channels: [
'twirl-channel'
]),
]);
}
}
bash
php artisan vendor:publish --provider="Moonshine\Twirl\Providers\TwirlServiceProvider"