Wie Du mit dem PHP Framework Laravel Anfängst deine Web-Application zu entwickeln
Das PHP Framework Laravel ist mitlerweile in der Version 9 verfügbar. Ich selbst nutze es seit Version 4 und bin seitdem absolut begeistert von den Möglichkeiten. In diesem Artikel werde ich dir verraten wie du Laravel zum Einsatz bringst und das am besten anhand eines Beispiels. Ich denke ein einfacher Blog, umgesetzt mit Laravel, sollte genau das richtige für den Einstieg sein. Dazu werde ich sicher mehr als einen Artikel benötigen, daher wird daraus eine kleine Serie. Dann Lass uns einfach anfangen.
System-voraussetzungen
Bevor wir loslegen können sind verschiedene Voraussetzungen zu schaffen. Laravel benötigt PHP einschließlich diverser Erweiterungen sowie idealerweise aber nicht zwingend eine Datenbank wie MySQL bzw. MariaDB (zur Speicherung der Daten) und einen Webserver wie zum Beispiel NGINX oder APACHE (zur Bereitstellung der Inhalte). Früher habe ich den apache Webserver verwendet seit 4 Jahren bin ich jedoch zu Nginx umgestiegen. Laravel selbst bietet einen einfachen Webserver über das Tool artisan mit an, der für die ersten Entwicklungsschritte völlig ausreichend ist. Aber legen wir jetzt nach und nach los. Ich selbst nutze gern Ubuntu oder Debain als Server-System und entwickle auf meinem Mac.
Schritt 1: Grundkonfiguration damit Du Laravel grundlegend starten kannst
Starte als erstes eine Konsole und prüfe ob bereits PHP auf deinem System vorhanden ist. Gib dazu php -v ein. Solltest du es bereits installiert haben erhältst du folgende Ausgabe.
# php -v
PHP 8.2.0 (cli) (built: Dec 9 2022 16:30:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies
#Falls nicht, installiere als erstes php auf deinem Rechner. Unter MacOs nutze idealerweise den Paketmanager homebrew dazu. Unter: https://brew.sh bekommst du erklärt wie du es auf deinen Mac installierst. Solltest ein Ubuntu oder Debian System nutzen benutze einfach den Paketmanager apt für die Installation von php. Eine gute Beschreibung um gleich alles am Stück zu installieren findest du hier: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-22-04.
Als nächstes prüfe ob auch den PHP Abhängigkeits-Manager Composer auf deinem System installiert hast. Dein Paketmanager wird in der Regel dir auch composer bereitstellen, falls nicht bekommst du die letzte Version und eine Beschreibung zur Installation auf https://getcomposer.org. Mit composer -v prüfe einfach, ob das Tool funktioniert.
# composer -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.5.1 2022-12-22 15:33:54
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer's internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [dumpautoload] Dumps the autoloader
exec Executes a vendored binary/script
fund Discover how to help fund the maintenance of your dependencies
global Allows running commands in the global composer dir ($COMPOSER_HOME)
help Display help for a command
init Creates a basic composer.json file in current directory
install [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licenses Shows information about licenses of dependencies
list List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock
Prüfe gleich noch ob auch MySql oder MariaDB richtig installiert sind mit mysql -u root -p, -p benötigst du falls du ein Passwort bereits gesetzt hast, falls nicht lass -p einfach weg. Der Standard-Nutze bei MySql ist meist root daher -u root.
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 239 Server version: 8.0.31 Homebrew Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Für Windownutzer empfehle ich das Tool XAMPP https://www.apachefriends.org/download.html es installiert alles nötige auf einem Windowssystem.
So sind die Vorarbeiten gemacht? Ja, dann lass uns als nächstest Laravel besorgen. Ich empfehle dir vorher noch ein extra Verzeichnis dafür anzulegen und über deine Console dich dahin zu bewegen. Als nächstest verwenden wir den Consolen-Befehl in einem Verzeichnis deiner Wahl: composer create-project laravel/laravel beispiel-blog
Du wirst folgende (gekürzte) Consolen-Ausgabe erhalten:
# composer create-project laravel/laravel beispiel-blog
Creating a "laravel/laravel" project at "./beispiel-blog"
Info from https://repo.packagist.org: #StandWithUkraine
Installing laravel/laravel (v9.5.1)
- Downloading laravel/laravel (v9.5.1)
- Installing laravel/laravel (v9.5.1): Extracting archive
Created project in /Users/alectronic/Sites/beispiel-blog
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
...
53 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
INFO Discovering packages.
laravel/sail ................................................................................................................................ DONE
laravel/sanctum ............................................................................................................................. DONE
laravel/tinker .............................................................................................................................. DONE
nesbot/carbon ............................................................................................................................... DONE
nunomaduro/collision ........................................................................................................................ DONE
nunomaduro/termwind ......................................................................................................................... DONE
spatie/laravel-ignition ..................................................................................................................... DONE
80 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
INFO No publishable resources for tag [laravel-assets].
No security vulnerability advisories found
> @php artisan key:generate --ansi
INFO Application key set successfully.
#
Das war es Laravel ist nun in deinem Verzeichnis installiert. Lass uns Laravel als nächstes starten, dazu bewege dich über die Console in das neu angelegte Verzeichnis hier "beispiel-blog" und dort führst du den Befehl php artisan serve aus.
# cd beispiel-blog # php artisan serve INFO Server running on [http://127.0.0.1:8000]. Press Ctrl+C to stop the server
Klasse! Nutze nun einen Webbrowser deiner Wahl und rufe dort die URL: http://127.0.0.1:8000 auf.
Im nächsten Blog werden wir uns das Framework Laravel ein wenig genauer ansehen und dann nach und nach mit Laravel einen Beispiel-Blog entwickeln.