LaraKnife Kochbuch: Unterschied zwischen den Versionen

Aus Vokabulabor
Zur Navigation springen Zur Suche springen
 
(7 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
= Links =
= Links =
[[Laraknife]]


= Zielsetzung =
= Zielsetzung =
Zeile 13: Zeile 14:
* Datenbank erweitern:
* Datenbank erweitern:
<pre>
<pre>
php artisan make:migration add_notes_info
php artisan make:migration add_notes_info --table=notes
</pre>
</pre>
Die entstehende Datei bearbeiten:
Die entstehende Datei bearbeiten:
Zeile 19: Zeile 20:
public function up()
public function up()
{
{
     Schema::create('notes', function ($table) {
     Schema::table('notes', function ($table) {
         $table->string("info");
         $table->string("info");
     });
     });
}
}
</pre>
* Auflistung der möglichen Datentypen: [[Datenbank-Laravel#Felddefinitionen]]
<pre>
php artisan migrate
</pre>
</pre>


Zeile 32: Zeile 37:
** Feld in edit.blade.php eintragen
** Feld in edit.blade.php eintragen
** Feld in show.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:
**** <code>ViewHelper::addConditionPattern($conditions, $parameters, 'title,body,info,', 'text');</code>
*** Wenn ein Combobox als Filter dazukommt:
**** <code>ViewHelper::addConditionComparism($conditions, $parameters, 'category_scope', 'category');</code>
**** <code>$optionsCategory = SProperty::optionsByScope('category', $fields['category'], 'all');</code>
**** <code>return view('note.index', [... optionsCategory ... ]);</code>
= Registerblatt-Seite =
* Controller.edit()
<syntaxhighlight lang="php">
$context = new ContextLaraKnife($request, null, $note);
$navigationTabInfo = ViewHelperLocal::getNavigationTabInfo('note-edit', 0, $note->id);
$rc = view('note.edit', [
  'context' => $context,
...
  'navTabsInfo' => $navigationTabInfo
]);
</syntaxhighlight>
* view/note/edit.blade.php
<pre>
@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
</pre>
* ViewHelperLocal.php:
<syntaxhighlight lang="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;
}
</syntaxhighlight>
= Email verschicken =
* neu: resources/views/mails/note-notification.blade.php
** Hier werden Variablen ($from ...) definiert, die als Map in EmailHelper::sendMail() übergeben werden (siehe unten).
<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>
* neu: app/Mail/NoteNotification.php:
** Die obige note-notifacation.blade.php-Datei wird mittels ... view: 'mails.note-notification' eingebunden
** Variablen können verwendet werden: $this->snippets['title'] referenziert den Eintrag aus obiger Map
<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>
* 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>
* Anwendung
** Hier müssen die obigen Variablen als Map übergeben werden
<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>

Aktuelle Version vom 30. Mai 2024, 17:46 Uhr

Links

Laraknife

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");
    });
}
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

  • neu: resources/views/mails/note-notification.blade.php
    • Hier werden Variablen ($from ...) definiert, die als Map in EmailHelper::sendMail() übergeben werden (siehe unten).
<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>
  • neu: app/Mail/NoteNotification.php:
    • Die obige note-notifacation.blade.php-Datei wird mittels ... view: 'mails.note-notification' eingebunden
    • Variablen können verwendet werden: $this->snippets['title'] referenziert den Eintrag aus obiger Map
<?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 [];
    }
}
  • 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;
    }
}
  • Anwendung
    • Hier müssen die obigen Variablen als Map übergeben werden
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")]);
}