Taskx mittels Laraknife: Unterschied zwischen den Versionen

Aus Vokabulabor
Zur Navigation springen Zur Suche springen
 
(47 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 2: Zeile 2:
* [[Laraknife]]
* [[Laraknife]]
* [[Laravel]]
* [[Laravel]]
* [[LaraKnife Kochbuch]]
* [[Deployment Laraknife Projekt]]


= Zielsetzung =
= Zielsetzung =
Zeile 15: Zeile 17:


= Installation =
= Installation =
* php muss installiert sein! [[PHP einrichten]] php-sqlite3
* mariadb muss installiert sein: [[mysql]]
* als normaler Benutzer (nicht root):
* als normaler Benutzer (nicht root):
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
SCRIPT=/tmp/CreateLaraProj.sh
PROJ=taskx
 
SCRIPT=/var/tmp/CreateLaraProj.sh
wget "https://public.hamatoma.de/public/CreateLaraProj.sh" -O $SCRIPT
wget "https://public.hamatoma.de/public/CreateLaraProj.sh" -O $SCRIPT
chmod +x $SCRIPT
chmod +x $SCRIPT
PROJ=taskx
PASSW=topsecret
PASSW=topsecret
BASE=/home/ws/php
BASE=/home/ws/php
sudo apt update
sudo apt install composer -y
$SCRIPT $PROJ "$PASSW" "$BASE"
$SCRIPT $PROJ "$PASSW" "$BASE"
</syntaxhighlight>
</syntaxhighlight>
* Am Ende wird dazu aufgefordert, das Script /tmp/IncludeLara.sh zu starten:
* Am Ende wird dazu aufgefordert, das Script /tmp/IncludeLara.sh zu starten:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
/tmp/IncludeLara.sh git $BASE/$PROJ
# wget https://public.hamatoma.de/public/IncludeLara.sh -O /tmp/IncludeLara.sh
# chmod +x /tmp/IncludeLara.sh
/tmp/IncludeLara.sh $BASE/$PROJ local
# oder
/tmp/IncludeLara.sh $BASE/$PROJ git
</syntaxhighlight>
</syntaxhighlight>


== Mehrsprachigkeit (I18N) ==
== Restliche Installation ==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
larascripts/laraknife-tool.sh init-i18n
cd $BASE/$PROJ
./Join
larascripts/laraknife-tool.sh rest
 
./Own.sh all
</syntaxhighlight>
</syntaxhighlight>
* Die neuen Übersetzungen müssen in resources/lang/sources/taskx.de.json eingetragen werden.


== Roles und SProperties füllen ==
Das folgende sind die einzelnen Schritte, '''normalerweise nicht''' erforderlich:
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# Links erstellen/auffrischen:
larascripts/laraknife-tool.sh build-links [--force]
# Mehrsprachigkeit
larascripts/laraknife-tool.sh init-i18n
# Die neuen Übersetzungen müssen in resources/lang/sources/taskx.de.json eingetragen werden.
# Roles und SProperties füllen
larascripts/laraknife-tool.sh fill-db
larascripts/laraknife-tool.sh fill-db
# Laraknife-Module anpassen
larascripts/laraknife-tool.sh adapt-modules
# Layout erstellen
larascripts/laraknife-tool.sh create-layout
# Homepage einrichten
larascripts/laraknife-tool.sh create-home
</syntaxhighlight>
</syntaxhighlight>


== Laraknife-Module anpassen ==
= Entwicklungsumgebung starten =
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
larascripts/laraknife-tool.sh adapt-modules
cd /home/ws/php/taskx
./Build
# In einem ANDEREN Terminal:
php artisan serve
</syntaxhighlight>
</syntaxhighlight>


= Modul Notes erstellen =
= Modul Device erstellen =
== Tabellenbeschreibung erstellen ==
== Tabellenbeschreibung erstellen ==
* Konvention: Tabelle wird kleingeschrieben, im Plural
* Konvention: Tabelle wird kleingeschrieben, im Plural
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
TABLE=notes
TABLE=devices
php artisan make:migration create_${TABLE}_table
php artisan make:migration create_${TABLE}_table
</syntaxhighlight>
</syntaxhighlight>
* Es wird die Datei database/migrations/2023_12_29_180821_create_notes_table.php erzeugt
* Es wird die Datei database/migrations/2023_12_29_180821_create_devices_table.php erzeugt, wobei Datum/Uhrzeit bei jeder Generierung anders ist.
* Diese Datei anpassen:
* Diese Datei anpassen:
<pre>
<pre>
         Schema::create('notes', function (Blueprint $table) {
         Schema::create('devices', function (Blueprint $table) {
             $table->id();
             $table->id();
             $table->timestamps();
             $table->timestamps();
             $table->string('title');
             $table->string('name');
             $table->text('body');
             $table->text('description');
            // foreign key of sproperties.
             $table->integer('devicegroup_scope');
             $table->integer('category_scope');
             $table->foreignId('owner_id')->nullable()->references('id')->on('users');
            // foreign key of sproperties.
             $table->integer('status_scope');
            $table->foreignId('user_id')->references('id')->on('users')->nullable();
         });
         });
    }
</pre>
</pre>
* Syntax für Feldänderungen: [[Datenbank-Laravel#Feld%C3%A4nderungen]]


== Modul erzeugen ==
== Modul erzeugen ==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
php artisan migrate
php artisan migrate
./Lara create:module database/migrations/*_create_notes_table.php
 
./Lara create:module database/migrations/*_create_${TABLE}_table.php
# oder, wenn das Modul nicht automatisch gebildet werden kann:
./Lara create:module database/migrations/*_create_${TABLE}_table.php --module=device
 
 
php artisan migrate
</syntaxhighlight>
</syntaxhighlight>


== Layout erstellen ==
== Database Seeder ==
* Datei resources/views/layouts/taskx.blade.php erstellen:
Um Tabellen mit sinnvollen Daten vorzubelegen, werden '''Database Seeder''' verwendet.
<syntaxhighlight lang="html">
<syntaxhighlight lang="bash">
larascripts/laraknife-tool.sh create-layout
php artisan make:seeder DeviceSeeder
</syntaxhighlight>
Die entstehende Datei database/seeders/DeviceSeeder.php anpassen: Icons in https://icons.getbootstrap.com
<pre>
    public function run(): void
    {
        SProperty::insertIfNotExists(2001, 'devicegroup', 'Computer', 10, 'C');
        SProperty::insertIfNotExists(2002, 'devicegroup', 'Router', 20, '$');
        Menuitem::insertIfNotExists('articles', 'bi bi-journals');
        Module::insertIfNotExists('Article');
    }
</pre>
Aktivieren:
<syntaxhighlight lang="bash">
php artisan db:seed --class=DeviceSeeder
</syntaxhighlight>
</syntaxhighlight>


Zeile 85: Zeile 133:
* In Datei routes/web.php einfügen:
* In Datei routes/web.php einfügen:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
SPropertyController::routes();
...
RoleController::routes();
DeviceController::routes();
UserController::routes();
...
NoteController::routes();
</syntaxhighlight>
</syntaxhighlight>
* Es sind 4 Namen unterstrichen: mit Strg-Alt-I Klassen einbinden.
* Die Klasse NodeController ist unterstrichen: mit Strg-Alt-I Klassen einbinden.


== Starten, Registrieren, Login ==
== Starten, Registrieren, Login ==
Zeile 96: Zeile 143:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
./Build
./Build
# in einem anderen Terminal:
php artisan serve
php artisan serve
</syntaxhighlight>
</syntaxhighlight>
Zeile 102: Zeile 150:
* Felder ausfüllen
* Felder ausfüllen
* Oben rechts: Login
* Oben rechts: Login
== In Homepage einfügen ==
* resources/views/home.blade.php:
<pre>
<li><a href="/device-index">Geräte</a></li>
</pre>


== Controller und Views anpassen ==
== Controller und Views anpassen ==
=== Übersicht (index) ===
=== Übersicht (index) ===
* Datei app/Http/Controllers/NoteController.php
* Datei app/Http/Controllers/DeviceController.php
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
     public function index()
     public function index()
     {
     {
        if (array_key_exists('btnSubmit', $_POST) && $_POST['btnSubmit'] === 'btnNew') {
            return redirect('/note-create');
        } else {
            $sql = 'SELECT id, title, body, user_id, cast(body AS VARCHAR(60)) as body2 FROM notes';
            $userId = auth()->id();
            $parameters = [':user_id' => $userId];
            $conditions = ['user_id = :user_id'];
            if (count($_POST) == 0) {
                $fields = [
                    'text' => '',
                    '_sortParams' => 'id:desc'
                ];
            } else {
                $fields = $_POST;
                ViewHelper::addConditionPattern($conditions, $parameters, 'title,body', 'text');
            }
            $sql = DbHelper::addConditions($sql, $conditions);
            $sql = DbHelper::addOrderBy($sql, $fields['_sortParams']);
            $records = DB::select($sql, $parameters);
            $pagination = new Pagination($sql, $parameters, $fields);
            return view('note.index', [
                'records' => $records,
                'fields' => $fields,
                'pagination' => $pagination
            ]);
        }
     }
     }
</syntaxhighlight>
</syntaxhighlight>
* Datei resources/views/note/index.blade.php
* Datei resources/views/device/index.blade.php
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
<form id="note-index" action="/note-index" method="POST">
<form id="device-index" action="/device-index" method="POST">
     @csrf
     @csrf
    <x-laraknife.index-panel title="{{ __('Notes') }}">
      <x-laraknife.filter-panel legend="{{ $pagination->legendText() }}">
        <x-laraknife.text position="alone" name="text" label="Text" value="{{$fields['text']}}" width2="4" />
      </x-laraknife.filter-panel>
      <x-laraknife.index-button-panel buttonType="new"/>
      <x-laraknife.sortable-table-panel :fields="$fields" :pagination="$pagination">
        <thead>
          <tr>
            <th></th>
            <th sortId="id">{{__('Id')}}</th>
            <th sortId="title">{{__('Title')}}</th>
            <th sortId="body">{{__('Body')}}</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
      @foreach ($records as $note)
        <tr>
          <td><a href="/note-edit/{{$note->id}}">{{ __('Change')}}</a></td>
          <td>{{$note->id}}</td>
          <td>{{$note->title}}</td>
          <td>{{$note->body2}}</td>
          <td><a href="/note-delete/{{$note->id}}">{{ __('Delete')}}</a></td>
        </tr>
      @endforeach
      </tbody>
    </x-laraknife.sortable-table-panel>
  </x-laraknife.index-panel>
</syntaxhighlight>
</syntaxhighlight>


=== Anlegen (create) ===
=== Anlegen (create) ===
* Datei app/Http/Controllers/NoteController.php
* Datei app/Http/Controllers/DeviceController.php
* Die drei Methoden '''ersetzen''':
* Die drei Methoden '''ersetzen''':
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
    public function create(Request $request)
    {
        if (array_key_exists('btnSubmit', $_POST) && $_POST['btnSubmit'] === 'btnCancel') {
            $rc = redirect('/note-index');
        } else {
            $rc = null;
            $error = null;
            if (count($_POST) > 0) {
                $fields = $_POST;
                try {
                    $incomingFields = $request->validate($this->rules(true));
                    $rc = $this->store($request);
                } catch (\Exception $e) {
                    $error = $e->getMessage();
                }
            } else {
                $fields = [
                    'title' => '',
                    'body' => '',
                    'category_scope' => '2001',
                    'status_scope' => '2601',
                    'group_scope' => '2501',
                ];
            }
            if ($rc == null) {
                $optionsCategory = SProperty::optionsByScope('category', $fields['category_scope'], '-');
                $optionsStatus = SProperty::optionsByScope('notestatus', $fields['status_scope'], '-');
                $optionsGroup = SProperty::optionsByScope('group', $fields['group_scope'], '-');
                $rc = view('note.create', ['fields' => $fields,
                    'optionsCategory' => $optionsCategory,
                    'optionsStatus' => $optionsStatus,
                    'optionsGroup' => $optionsGroup,
                    'error' => $error]);
            }
        }
        return $rc;
    }
    private function rules(bool $isCreation=false): array
    {
        $rc = [
            'title' => 'required',
            'body' => 'required',
            'category_scope' => 'required',
            'status_scope' => 'required',
            'group_scope' => 'required',
        ];
        return $rc;
    }
    public function store(Request $request)
    {
        if ($request->btnSubmit === 'btnStore') {
            $incomingFields = $request->validate($this->rules());
            $incomingFields['user_id'] = auth()->id();
            $incomingFields['body'] = strip_tags($incomingFields['body']);
            Note::create($incomingFields);
        }
        return redirect('/note-index');
    }
</syntaxhighlight>
</syntaxhighlight>


* Datei resources/views/note/create.blade.php
* Datei resources/views/device/create.blade.php
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
    <form id="note-create" action="/note-create" method="POST">
        @csrf
        <x-laraknife.create-panel title="{{ __('Creation of a Note') }}" error="{{$error}}">
            <x-laraknife.combobox position="first" name="status_scope" label="Status" value="{{$fields['status_scope']}}" :options="$optionsStatus" width2="4" />
            <x-laraknife.combobox position="last" name="group_scope" label="Group" value="{{$fields['group_scope']}}" :options="$optionsGroup" width2="4" />
            <x-laraknife.combobox position="alone" name="category_scope" label="Category" value="{{$fields['category_scope']}}" :options="$optionsCategory" width2="4" />
            <x-laraknife.text position="alone" name="title" label="Title" value="{{$fields['title']}}"  />
            <x-laraknife.bigtext position="alone" name="body" label="Body" value="{{$fields['body']}}" rows="10" />
        </x-laraknife.create-panel>
    </form>
</syntaxhighlight>
</syntaxhighlight>
* Übersetzungen in resources/lang/sources/taskx.de.json
* Übersetzungen in resources/lang/sources/taskx.de.json
<pre>
<pre>
"Body": "Info",
"Devices": "Geräte",
"Category": "Kategorie",
"Creation of a Note": "Anlegen einer Notiz",
"Group": "Gruppe",
"open": "offen",
"standard": "Standard",
"Status": "Status",
"Title": "Titel",
</pre>
</pre>
* Aktivieren:
* Aktivieren:
Zeile 265: Zeile 190:


=== Ändern (edit) ===
=== Ändern (edit) ===
* Datei app/Http/Controllers/NoteController.php
* Datei app/Http/Controllers/DeviceController.php
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
     public function edit(Note $note)
     public function edit(Device $device)
     {
     {
        if (array_key_exists('btnSubmit', $_POST) && $_POST['btnSubmit'] === 'btnCancel') {
            $rc = redirect('/note-index');
        } else {
            $optionsCategory = SProperty::optionsByScope('category', $note->category_scope, '-');
            $optionsStatus = SProperty::optionsByScope('notestatus', $note->status_scope, '-');
            $optionsGroup = SProperty::optionsByScope('group', $note->group_scope, '-');
            $rc = view('note.edit', [
                'note' => $note,
                'optionsCategory' => $optionsCategory,
                'optionsStatus' => $optionsStatus,
                'optionsGroup' => $optionsGroup,
            ]);
        }
        return $rc;
     }
     }
     public function update(Note $note, Request $request)
     public function update(Device $device, Request $request)
     {
     {
         $rc = null;
         $rc = null;
        if ($request->btnSubmit === 'btnStore') {
            try {
                $incomingFields = $request->validate($this->rules());
                $incomingFields['body'] = strip_tags($incomingFields['body']);
                $note->update($incomingFields);
                $rc = redirect('/note-index');
            } catch (\Exception $exc) {
                $msg = $exc->getMessage();
                $rc = back();
            }
        }
        if ($rc == null) {
            $rc = redirect('/note-index');
        }
         return $rc;
         return $rc;
     }
     }
</syntaxhighlight>
</syntaxhighlight>


* Datei resources/views/note/edit.blade.php
* Datei resources/views/device/edit.blade.php
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
    <form id="note-edit" action="/note-update/{{ $note->id }}" method="POST">
        @csrf
        <x-laraknife.edit-panel title="{{ __('Change of a Note') }}">
            <x-laraknife.combobox position="first" name="status_scope" label="Status" value="{{$note->status_scope}}" :options="$optionsStatus" width2="4" />
            <x-laraknife.combobox position="last" name="group_scope" label="Group" value="{{$note->group_scope}}" :options="$optionsGroup" width2="4" />
            <x-laraknife.combobox position="alone" name="category_scope" label="Category" value="{{$note->category_scope}}" :options="$optionsCategory" width2="4" />
            <x-laraknife.text position="alone" name="title" label="Title" value="{{$note->title}}"  />
            <x-laraknife.bigtext position="alone" name="body" label="Body" value="{{$note->body}}" rows="10" />
        </x-laraknife.edit-panel>
    </form>
</syntaxhighlight>
</syntaxhighlight>


* Übersetzungen in resources/lang/sources/taskx.de.json
* Übersetzungen in resources/lang/sources/taskx.de.json
<pre>
<pre>
"Change of a Note": "Ändern einer Notiz",
"Change of a Device": "Ändern eines Gerätes",
</pre>
</pre>
* Aktivieren:
* Aktivieren:
Zeile 329: Zeile 216:


=== Anzeigen (show/delete) ===
=== Anzeigen (show/delete) ===
* Datei app/Http/Controllers/NoteController.php
* Datei app/Http/Controllers/DeviceController.php
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
     public function show(Note $note)
     public function show(Device $device)
     {
     {
        if (array_key_exists('btnSubmit', $_POST) && $_POST['btnSubmit'] === 'btnCancel') {
            $rc = redirect('/note-index');
        } else {
            $optionsCategory = SProperty::optionsByScope('category', 'category', '');
            $optionsGroup = SProperty::optionsByScope('group', 'group', '');
            $optionsStatus = SProperty::optionsByScope('notestatus', 'status', '');
            $rc = view('note.show', [
                'note' => $note,
                'optionsCategory' => $optionsCategory,
                'optionsGroup' => $optionsGroup,
                'optionsStatus' => $optionsStatus,
                'mode' => 'delete'
            ]);
        }
        return $rc;
     }
     }
</syntaxhighlight>
</syntaxhighlight>


* Datei resources/views/note/show.blade.php
* Datei resources/views/device/show.blade.php
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
    <form id="note-show" action="/note-show/{{ $note->id }}/{{ $mode }}" method="POST">
        @csrf
        @if ($mode === 'delete')
            @method('DELETE')
        @endif
        <x-laraknife.show-panel title="{{ __($mode !== 'delete' ? 'A Note' : 'Deletion of a Note') }}"
            mode="{{ $mode }}">
            <x-laraknife.combobox position="first" name="status_scope" label="Status" value="{{ $note->status_scope }}"
                :options="$optionsStatus" width2="4" attribute="readonly" />
            <x-laraknife.combobox position="last" name="group_scope" label="Group" value="{{ $note->group_scope }}"
                :options="$optionsGroup" width2="4" attribute="readonly" />
            <x-laraknife.combobox position="first" name="category_scope" label="Category" value="{{ $note->category_scope }}"
                :options="$optionsCategory" width2="4" attribute="readonly" />
            <x-laraknife.text position="last" name="id" label="Id" value="{{ $note->id }}" width2="4"
                attribute="readonly" />
            <x-laraknife.text position="alone" name="title" label="Title" value="{{ $note->title }}"
                attribute="readonly" />
            <x-laraknife.bigtext position="alone" name="body" label="Body" value="{{ $note->body }}" rows="10"
                attribute="readonly" />
        </x-laraknife.show-panel>
    </form>
</syntaxhighlight>
</syntaxhighlight>
* Übersetzungen in resources/lang/sources/taskx.de.json
* Übersetzungen in resources/lang/sources/taskx.de.json
<pre>
<pre>
"A Note" : "Eine Notiz",
"A Device" : "Ein Gerät",
"Deletion of a Note": "Löschen einer Notiz",
"Deletion of a Device": "Löschen eines Gerätes",
</pre>
</pre>
* Aktivieren:
* Aktivieren:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
./Join
./Join
</syntaxhighlight>
= Neue Instanz von taskx einrichten =
* Lokal:
<syntaxhighlight lang="bash">
PROJ=taskx
BASE=/home/ws/php
cd $BASE
git clone ssh://git@git.hamatoma.de:/home/git/repo/$PROJ.git
cd $PROJ
scripts/InitApp.sh
</syntaxhighlight>
* auf einem Server:
<syntaxhighlight lang="bash">
PROJ=taskx
DOMAIN=$PROJ.hamatoma.de
BASE=/srv/www
cd $BASE
sudo -u www-data git config --global --add safe.directory /home/git/repo/$PROJ.git
sudo -u www-data git clone /home/git/repo/$PROJ.git
mv $PROJ $DOMAIN
cd $DOMAIN
sudo -u www-data git checkout main
../laraknife/scripts/InstallInitApp.sh
echo "DEV_USER=www-data" >.env.user
./Own.sh all
cp .env.example .env
vi .env
bash scripts/InitApp.sh
sudo -u www-data ./Build prod
sudo -u www-data ./artisan key:generate
</syntaxhighlight>
</syntaxhighlight>

Aktuelle Version vom 3. Januar 2025, 21:21 Uhr

Links

Zielsetzung

Es soll eine minimale Web-Applikation mit den Werkzeugen Laravel und Laraknife erstellt werden: Eine Verwaltung von Notizen.

Name

Eine Verballhornung des Wortes "Tasks": Die schwäbische Aussprache spricht ein x am Ende.

Eigenschaften

  • Benutzerverwaltung: nur angemeldete Benutzer können Applikation nutzen
  • Rechteverwaltung mittels Rollen
  • Verwaltung von Notizen: Titel, Text, Kategorie, Status: offen, erledigt

Installation

  • php muss installiert sein! PHP einrichten php-sqlite3
  • mariadb muss installiert sein: mysql
  • als normaler Benutzer (nicht root):
PROJ=taskx

SCRIPT=/var/tmp/CreateLaraProj.sh
wget "https://public.hamatoma.de/public/CreateLaraProj.sh" -O $SCRIPT
chmod +x $SCRIPT
PASSW=topsecret
BASE=/home/ws/php
sudo apt update
sudo apt install composer -y

$SCRIPT $PROJ "$PASSW" "$BASE"
  • Am Ende wird dazu aufgefordert, das Script /tmp/IncludeLara.sh zu starten:
# wget https://public.hamatoma.de/public/IncludeLara.sh -O /tmp/IncludeLara.sh
# chmod +x /tmp/IncludeLara.sh
/tmp/IncludeLara.sh $BASE/$PROJ local
# oder
/tmp/IncludeLara.sh $BASE/$PROJ git

Restliche Installation

cd $BASE/$PROJ
larascripts/laraknife-tool.sh rest

./Own.sh all

Das folgende sind die einzelnen Schritte, normalerweise nicht erforderlich:

# Links erstellen/auffrischen:
larascripts/laraknife-tool.sh build-links [--force]
# Mehrsprachigkeit
larascripts/laraknife-tool.sh init-i18n
# Die neuen Übersetzungen müssen in resources/lang/sources/taskx.de.json eingetragen werden.
# Roles und SProperties füllen
larascripts/laraknife-tool.sh fill-db
# Laraknife-Module anpassen
larascripts/laraknife-tool.sh adapt-modules
# Layout erstellen
larascripts/laraknife-tool.sh create-layout
# Homepage einrichten
larascripts/laraknife-tool.sh create-home

Entwicklungsumgebung starten

cd /home/ws/php/taskx
./Build
# In einem ANDEREN Terminal:
php artisan serve

Modul Device erstellen

Tabellenbeschreibung erstellen

  • Konvention: Tabelle wird kleingeschrieben, im Plural
TABLE=devices
php artisan make:migration create_${TABLE}_table
  • Es wird die Datei database/migrations/2023_12_29_180821_create_devices_table.php erzeugt, wobei Datum/Uhrzeit bei jeder Generierung anders ist.
  • Diese Datei anpassen:
        Schema::create('devices', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name');
            $table->text('description');
            $table->integer('devicegroup_scope');
            $table->foreignId('owner_id')->nullable()->references('id')->on('users');
        });
    }

Modul erzeugen

php artisan migrate

./Lara create:module database/migrations/*_create_${TABLE}_table.php
# oder, wenn das Modul nicht automatisch gebildet werden kann:
./Lara create:module database/migrations/*_create_${TABLE}_table.php --module=device


php artisan migrate

Database Seeder

Um Tabellen mit sinnvollen Daten vorzubelegen, werden Database Seeder verwendet.

php artisan make:seeder DeviceSeeder

Die entstehende Datei database/seeders/DeviceSeeder.php anpassen: Icons in https://icons.getbootstrap.com

    public function run(): void
    {
        SProperty::insertIfNotExists(2001, 'devicegroup', 'Computer', 10, 'C');
        SProperty::insertIfNotExists(2002, 'devicegroup', 'Router', 20, '$');
        Menuitem::insertIfNotExists('articles', 'bi bi-journals');
        Module::insertIfNotExists('Article');
    }

Aktivieren:

php artisan db:seed --class=DeviceSeeder

Routing einrichten

  • In Datei routes/web.php einfügen:
...
DeviceController::routes();
...
  • Die Klasse NodeController ist unterstrichen: mit Strg-Alt-I Klassen einbinden.

Starten, Registrieren, Login

  • Den Webserver starten:
./Build
# in einem anderen Terminal:
php artisan serve

In Homepage einfügen

  • resources/views/home.blade.php:
<li><a href="/device-index">Geräte</a></li>

Controller und Views anpassen

Übersicht (index)

  • Datei app/Http/Controllers/DeviceController.php
    public function index()
    {
    }
  • Datei resources/views/device/index.blade.php
<form id="device-index" action="/device-index" method="POST">
    @csrf

Anlegen (create)

  • Datei app/Http/Controllers/DeviceController.php
  • Die drei Methoden ersetzen:
  • Datei resources/views/device/create.blade.php
  • Übersetzungen in resources/lang/sources/taskx.de.json
"Devices": "Geräte",
  • Aktivieren:
./Join

Ändern (edit)

  • Datei app/Http/Controllers/DeviceController.php
    public function edit(Device $device)
    {
    }
    public function update(Device $device, Request $request)
    {
        $rc = null;
        return $rc;
    }
  • Datei resources/views/device/edit.blade.php
  • Übersetzungen in resources/lang/sources/taskx.de.json
"Change of a Device": "Ändern eines Gerätes",
  • Aktivieren:
./Join

Anzeigen (show/delete)

  • Datei app/Http/Controllers/DeviceController.php
    public function show(Device $device)
    {
    }
  • Datei resources/views/device/show.blade.php
  • Übersetzungen in resources/lang/sources/taskx.de.json
"A Device" : "Ein Gerät",
"Deletion of a Device": "Löschen eines Gerätes",
  • Aktivieren:
./Join

Neue Instanz von taskx einrichten

  • Lokal:
PROJ=taskx
BASE=/home/ws/php
cd $BASE
git clone ssh://git@git.hamatoma.de:/home/git/repo/$PROJ.git
cd $PROJ
scripts/InitApp.sh
  • auf einem Server:
PROJ=taskx
DOMAIN=$PROJ.hamatoma.de
BASE=/srv/www
cd $BASE
sudo -u www-data git config --global --add safe.directory /home/git/repo/$PROJ.git
sudo -u www-data git clone /home/git/repo/$PROJ.git
mv $PROJ $DOMAIN
cd $DOMAIN
sudo -u www-data git checkout main
../laraknife/scripts/InstallInitApp.sh

echo "DEV_USER=www-data" >.env.user
./Own.sh all

cp .env.example .env
vi .env
bash scripts/InitApp.sh

sudo -u www-data ./Build prod
sudo -u www-data ./artisan key:generate