-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Add Livewire support to the HTTP server #40
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example Componentresources/views/components/layouts/app.blade.php <!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ $title ?? 'Laracord' }}</title>
<script src="https://cdn.tailwindcss.com"></script>
@livewireStyles
</head>
<body>
{{ $slot }}
@livewireScripts
</body>
</html> app/Livewire/Message.php <?php
namespace App\Livewire;
use Laracord\HasLaracord;
use Livewire\Component;
class Message extends Component
{
use HasLaracord;
/**
* The selected user.
*
* @var string
*/
public $user;
/**
* The message to send.
*
* @var string
*/
public $message;
/**
* The members of the server.
*
* @var array
*/
public $members;
/**
* Mount the component.
*
* @return void
*/
public function mount()
{
$this->members = collect($this->discord()->users->map(fn ($user) => [
'id' => $user->id,
'username' => $user->username,
]))->keyBy('id');
}
/**
* Render the component.
*
* @return \Illuminate\View\View
*/
public function render()
{
return view('livewire.message', [
'members' => $this->members,
]);
}
/**
* Send a message to the selected user.
*
* @return void
*/
public function sendMessage()
{
$this->validate([
'user' => 'required',
'message' => 'required',
]);
if (! $this->members->has($this->user)) {
$this->addError('user', 'The selected user does not exist.');
return;
}
$this->message($this->message)->sendTo($this->user);
$this->reset(['user', 'message']);
session()->flash('message', 'Message sent!');
}
} resources/views/livewire/message.blade.php <div class="p-8 max-w-5xl mx-auto">
<h1 class="text-3xl mb-6">Laracord with Livewire</h1>
<h2 class="mb-2 text-xl">Send a Message</h2>
<div class="grid gap-8">
<select wire:model="user" class="border px-3 py-2 max-w-lg">
<option value="">Select a member...</option>
@foreach ($members as $member)
<option value="{{ $member['id'] }}">{{ $member['username'] }}</option>
@endforeach
</select>
@error('user') <span class="text-red-500">{{ $message }}</span> @enderror
<textarea class="border px-3 py-2" wire:model="message" rows="5" placeholder="Enter a message..."></textarea>
@error('message') <span class="text-red-500">{{ $message }}</span> @enderror
<button class="bg-blue-500 text-white px-4 py-2" wire:click="sendMessage">
Send Message
</button>
@session('message')
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative" role="alert">
<strong class="font-bold">Success!</strong>
<span class="block sm:inline">{{ session('message') }}</span>
</div>
@endsession
</div>
</div> app/Bot.php <?php
namespace App;
use App\Livewire\Message;
use Illuminate\Support\Facades\Route;
use Laracord\Laracord;
class Bot extends Laracord
{
/**
* The HTTP routes.
*/
public function routes(): void
{
Route::get('/message', Message::class)->name('message');
}
} Screenshots |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR finalizes the ReactPHP HTTP server/routing implementation adding support for Livewire 3.
Usage
Generate an application key:
Install Livewire:
Add the Livewire provider to
config/app.php
:Afterwards, you should be able to create a Livewire app layout/components and register them inside of
routes()
inapp/Bot.php
.