LaraKnife Kochbuch: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 96: | Zeile 96: | ||
return $rc; | return $rc; | ||
} | } | ||
</syntaxhighlight> | |||
= Email verschicken = | |||
* Anwendung | |||
<syntaxhighlight lang="php"> | |||
private function sendEmail(int $userId, Note $note){ | |||
$user = User::find($userId); | |||
EmailHelper::sendMail('note.notification', $user->email, ['name' => $user->name, 'title' => $note->title, 'contents' => $note->body, | |||
'from' => auth()->user()->name, 'link' => ViewHelper::buildLink("/note-edit/$note->id")]); | |||
} | |||
</syntaxhighlight> | |||
* app/Helpers/EmailHelper (Anpassung): | |||
<syntaxhighlight lang="php"> | |||
public static function sendMail(string $name, string $to, array $snippets) | |||
{ | |||
switch ($name) { | |||
... | |||
case 'note.notification': | |||
Mail::to($to)->send(new NoteNotification($snippets)); | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
</syntaxhighlight> | |||
* neu: app/Mail/NoteNotification.php: | |||
<syntaxhighlight lang="php"> | |||
<?php | |||
namespace App\Mail; | |||
use Illuminate\Bus\Queueable; | |||
use Illuminate\Contracts\Queue\ShouldQueue; | |||
use Illuminate\Mail\Mailable; | |||
use Illuminate\Mail\Mailables\Content; | |||
use Illuminate\Mail\Mailables\Envelope; | |||
use Illuminate\Queue\SerializesModels; | |||
class NoteNotification extends Mailable | |||
{ | |||
use Queueable, SerializesModels; | |||
private string $link; | |||
/** | |||
* Create a new message instance. | |||
*/ | |||
public function __construct(array $snippets) | |||
{ | |||
$this->snippets = $snippets; | |||
} | |||
/** | |||
* Get the message envelope. | |||
*/ | |||
public function envelope(): Envelope | |||
{ | |||
return new Envelope( | |||
subject: __('Responsibility changed') . ': ' . $this->snippets['title'], | |||
); | |||
} | |||
/** | |||
* Get the message content definition. | |||
*/ | |||
public function content(): Content | |||
{ | |||
return new Content( | |||
view: 'mails.note-notification', | |||
with: $this->snippets | |||
); | |||
} | |||
/** | |||
* Get the attachments for the message. | |||
* | |||
* @return array<int, \Illuminate\Mail\Mailables\Attachment> | |||
*/ | |||
public function attachments(): array | |||
{ | |||
return []; | |||
} | |||
} | |||
</syntaxhighlight> | |||
* neu: resources/views/mails/note-notification.blade.php | |||
<syntaxhighlight lang="html"> | |||
<p>{{ __('Hello')}} {{ $name }}, </p> | |||
<p>{{__('The responsibility has been transferred to you')}} ({{ $from }}).<br/> | |||
<a href="{{$link}}">{{$link}}</a></p> | |||
<hr> | |||
<h3>{{$title}}</h3> | |||
<p>{{$contents}} | |||
</p> | |||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 30. Mai 2024, 17:38 Uhr
Links
Zielsetzung
Kleine Tipps zur Verwendung von LaraKnife.
Moduländerungen
In den Beispielen wird das Modul Notes verwendet.
Zusätzliches Attribut
Es soll das Attribut "info" ergänzt werden:
Zu tun:
- Datenbank erweitern:
php artisan make:migration add_notes_info --table=notes
Die entstehende Datei bearbeiten:
public function up() { Schema::table('notes', function ($table) { $table->string("info"); }); }
- Auflistung der möglichen Datentypen: Datenbank-Laravel#Felddefinitionen
php artisan migrate
- Model erweitern:
- app/Models/Note.php: in $fillable eintragen
- Views erweitern:
- Feld eventuell in index.blade.php eintragen
- Feld in create.blade.php eintragen
- Feld in edit.blade.php eintragen
- Feld in show.blade.php eintragen
- NoteController anpassen:
- function create() und edit() und show(): ... $fields = [ ... 'info' => ...];
- function index():
- Wenn ein Filter dazukommt: $fields = [ ... 'info' => ...]
- Wenn bei einem gemeinsamen Suchfilter auch diese Spalte durchsucht werden soll:
ViewHelper::addConditionPattern($conditions, $parameters, 'title,body,info,', 'text');
- Wenn ein Combobox als Filter dazukommt:
ViewHelper::addConditionComparism($conditions, $parameters, 'category_scope', 'category');
$optionsCategory = SProperty::optionsByScope('category', $fields['category'], 'all');
return view('note.index', [... optionsCategory ... ]);
Registerblatt-Seite
- Controller.edit()
$context = new ContextLaraKnife($request, null, $note);
$navigationTabInfo = ViewHelperLocal::getNavigationTabInfo('note-edit', 0, $note->id);
$rc = view('note.edit', [
'context' => $context,
...
'navTabsInfo' => $navigationTabInfo
]);
- view/note/edit.blade.php
@extends('layouts.backend') @section('content') <form id="note-edit" action="/note-update/{{ $context->model->id }}" method="POST"> @csrf <x-laraknife.panels.standard title="{{ __('Change of a Note') }}" fieldset="false"> <x-laraknife.layout.nav-tabs :info="$navTabsInfo" fieldset="true"> <x-laraknife.forms.combobox position="first" name="category_scope" label="Category" :options="$optionsCategory" value="{{ $context->valueOf('category_scope') }}" width2="4" /> ... </x-laraknife.layout.nav-tabs> </x-laraknife.panels.edit> </form> @endsection
- ViewHelperLocal.php:
public static function getNavigationTabInfo(string $name, int $indexActive,
int $referenceId = 0): ?NavigationTabs
{
$rc = null;
switch ($name) {
case 'note-edit':
$rc = new NavigationTabs(["Properties;/note-edit/$referenceId",
"Documents;/note-index_documents/$referenceId"
], $indexActive);
break;
default:
break;
}
return $rc;
}
Email verschicken
- Anwendung
private function sendEmail(int $userId, Note $note){
$user = User::find($userId);
EmailHelper::sendMail('note.notification', $user->email, ['name' => $user->name, 'title' => $note->title, 'contents' => $note->body,
'from' => auth()->user()->name, 'link' => ViewHelper::buildLink("/note-edit/$note->id")]);
}
- app/Helpers/EmailHelper (Anpassung):
public static function sendMail(string $name, string $to, array $snippets)
{
switch ($name) {
...
case 'note.notification':
Mail::to($to)->send(new NoteNotification($snippets));
break;
default:
break;
}
}
- neu: app/Mail/NoteNotification.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class NoteNotification extends Mailable
{
use Queueable, SerializesModels;
private string $link;
/**
* Create a new message instance.
*/
public function __construct(array $snippets)
{
$this->snippets = $snippets;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: __('Responsibility changed') . ': ' . $this->snippets['title'],
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mails.note-notification',
with: $this->snippets
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
- neu: resources/views/mails/note-notification.blade.php
<p>{{ __('Hello')}} {{ $name }}, </p>
<p>{{__('The responsibility has been transferred to you')}} ({{ $from }}).<br/>
<a href="{{$link}}">{{$link}}</a></p>
<hr>
<h3>{{$title}}</h3>
<p>{{$contents}}
</p>