Symfony 7.3 einrichten

Version 7.3 von Symfony-Framework mit PHP 8.3

Inhaltsverzeichnis

Installieren

PHP 8.3

Zuerst muss PHP 8.3 installiert werden :

sudo apt-get update && apt-get upgrade -y

curl -fsSL https://packages.sury.org/php/apt.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/php.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt updatesudo apt-get install curl

sudo apt install -y php8.3 php8.3-cli php8.3-common php8.3-fpm php8.3-mbstring php8.3-xml php8.3-curl php8.3-mysql php8.3-intl php8.3-zip php8.3-opcache php8.3-gd php8.3-bcmath php8.3-readline unzip

Apache 2

Nun Webserver z.B. Apache2 und Datenbank-Zugang installieren :

sudo apt-get update && apt-get upgrade -y
sudo apt install -y apache2 libapache2-mod-php8.3
sudo systemctl enable apache2
sudo apt install -y mariadb-server
sudo systemctl enable mariadb
sudo mysql_secure_installation
sudo apt-get install phpmyadmin
sudo apt-get install curl

Dann empfiehlt es sich, einen virtuelen Host einzurichten:

<VirtualHost *:80>
    ServerName symfony.ubuntu.local
    ServerAlias symfony.ubuntu.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/mydemo/web

    <Directory /var/www/html/mydemo/web>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache den neuen virtuellen Host anmelden:

sudo a2ensite symfony.ubuntu.local.conf

Nicht vergessen: Hosteintragung (127.0.0.1 symfony.ubuntu.local ubuntu.local) zu machen und Apache neustarten:

sudo service apache2 restart
# Ggf. verhindern, dass Inhalt des Verzeichnisses angezeigt wird, wenn keine Indexdatei vorhanden ist:
sudo a2dismod autoindex

Symfony installieren

Im Verzeichnis /var/www/html folgende Befehle ausführen:

sudo su
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version
curl -sS https://get.symfony.com/cli/installer | bash
sudo mv ~/.symfony*/bin/symfony /usr/local/bin/symfony
symfony check:requirements

# Installation prüfen:
symfony

Projekt erzeugen

Symfony installieren

Hierbei werden nach diversen Eingaben gefragt.

cd /var/www/html/
symfony new my_project_name --webapp --no-git
cd my_project_name

Datenbankschema aktualisieren

# Datenbankverarbeitung prüfen
php bin/console doctrine:schema:update --dump-sql
# Datenbankstruktur aktualisieren
php bin/console doctrine:schema:update --force

Controller erstellen

php bin/console generate:controller --controller=Blogpost

Entities erstellen

Hierbei werden die Felder für die Datenbank definiert. Als Resultat erhält man eine PHP-Klasse.

php bin/console doctrine:generate:entity --entity=EntityDemo

Ressourcen einfügen

Unter /var/www/html/my_project_name/src/assets/public können diversen Zusätze (Framework wie Bootstrap) eingefügt werden, welche danach im Web-Verzeichnis mit dem nachfolgenden Befehl verlinkt wird. Damit steht es für die Webanwendung zur Verfügung.

php bin/console assets:install --symlink --relative

Beispiel TWIG-Template

base.html.twig:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset={{ _charset }}"/>
		<meta name="robots" content="noindex,nofollow"/>
		<title>{% block title %}DVD-Datenbank{% endblock %}</title>
		<link rel="stylesheet" href="{{ asset('bundles/mydemo/bootstrap/css/bootstrap.min.css') }}"/>
		<link rel="stylesheet" href="{{ asset('bundles/mydemo/bootstrap/css/bootstrap-theme.min.css') }}"/>
		<link rel="stylesheet" href="{{ asset('bundles/mydemo/dvd-db/css/main.css') }}"/>
		{% block head %}{% endblock %}
	</head>
	<body role="document">
 
		<!-- navbar -->
		<nav class="navbar navbar-default">
			<div class="container">
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed"
							data-toggle="collapse" data-target="#menu-toggle">
						<span class="sr-only"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="/">DVD-Datenbank</a>
				</div>
 
				<!-- Collect the nav links, forms, and other content for toggling -->
				<div class="collapse navbar-collapse" id="menu-toggle">
					<ul class="nav navbar-nav">
						<li><a href="#">Film hinzufügen</a></li>
					</ul>
				</div>
				<!-- /.navbar-collapse -->
			</div>
			<!-- /.container-fluid -->
		</nav>
		<!-- /navbar -->
 
		<div class='container'>
			{% block body %}{% endblock %}
		</div>
 
		{% block javascripts %}
			<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
			<script src="{{ asset('bundles/mydemo/bootstrap/js/bootstrap.min.js') }}" type="text/javascript"></script>
		{% endblock %}
 
	</body>
 
</html>

index.html.twig:

{% extends 'MyDemoBundle::base.html.twig' %}
{% block body %}
	<div class="row">			
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">Suche</div>
					<div class="panel-body">
					<div class="input-group">
						<input type="text" class="form-control">
						<div class="input-group-btn">
							<button class="btn btn-default" type="button">Go!</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="row">			
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading">Filme</div>
				<div class="panel-body">
					<p>Noch keine Filme vorhanden</p>
				</div>
				<table class="table">
					<thead>
						<tr>
							<th>Name</th>
							<th>Sprache</th>
							<th>Schauspieler</th>
							<th>Jahr</th>
							<th>IMDB ID</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</div>
{% endblock %}