PHP:Twigの基本

Twigの基本

{% ... %} ステートメント
{{ }} 実行結果の出力
{# #} コメント

PHP:Twigのインストール

公開:2026.04.26(日) 01:25

PHPテンプレートエンジン twig

PHPのテンプレートエンジン、HTML_Template_FlexyやSmartyの代替をChatGPTに聞いたところ "Twig" と "Blade" というのを教えてもらった。
ということで、今回はTwigというのをインストールしてみる。
インストールはChatGPTに聞いた手順で行う。

Twigとは

「ついっぐ」と呼ぶらしい。

環境

◆ Windows 11 25H2 (26200.8037) + WSL + Docker
◆ WSL:
PS C:\Users\xxx> wsl --version
WSL バージョン: 2.3.24.0
カーネル バージョン: 5.15.153.1-2
WSLg バージョン: 1.0.65
MSRDC バージョン: 1.2.5620
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26100.1-240331-1435.ge-release
Windows バージョン: 10.0.26200.8037
◆ Ubuntu:
xxx@xxx:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble

過去記事:Docker 学習 #4:Docker Composeを学ぶ で作成したPHPコンテナを使用する。
以下コマンドでコンテナを起動。
docker compose up -d

コンテナのbashに接続する。
docker exec -it (CONTAINER ID) /bin/bash
※ (CONTAINER ID)は "docker ps" コマンドで確認。

インストール

以下コマンドでインストール
composer require twig/twig

実行結果:→ 失敗
root@05fa4e42bbcd:~# composer require twig/twig
Xdebug: [Log Files] File '/tmp/xdebug.log' could not be opened.
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.25.0.1:9003 (through xdebug.client_host/xdebug.client_port).
./composer.json has been created
Running composer update twig/twig
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
  - Locking symfony/deprecation-contracts (v3.6.0)
  - Locking symfony/polyfill-ctype (v1.36.0)
  - Locking symfony/polyfill-mbstring (v1.36.0)
  - Locking twig/twig (v3.24.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
    Failed to download symfony/polyfill-mbstring from dist: The zip extension and unzip/7z commands are both missing, skipping.
Your command-line PHP is using multiple ini files. Run `php --ini` to show them.
    Now trying to download from source

In GitDownloader.php line 82:

  git was not found in your PATH, skipping source download


require [--dev] [--dry-run] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--fixed] [--no-suggest] [--no-progress] [--no-update] [--no-install] [--no-audit] [--audit-format AUDIT-FORMAT] [--no-security-blocking] [--update-no-dev] [-w|--update-with-dependencies] [-W|--update-with-all-dependencies] [--with-dependencies] [--with-all-dependencies] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [-m|--minimal-changes] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--] [...]
  

git, unzip, zipをインストール

1. apt-get update
実行結果:
root@05fa4e42bbcd:~# apt-get update
Hit:1 http://deb.debian.org/debian trixie InRelease
Get:2 http://deb.debian.org/debian trixie-updates InRelease [47.3 kB]
Get:3 http://deb.debian.org/debian-security trixie-security InRelease [43.4 kB]
Get:4 http://deb.debian.org/debian-security trixie-security/main amd64 Packages [127 kB]
Fetched 218 kB in 0s (1377 kB/s)
Reading package lists... Done
    
2. apt-get install git
実行結果:
root@05fa4e42bbcd:~# apt-get install git
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  git-man less libcbor0.10 libcurl3t64-gnutls libedit2 liberror-perl libfido2-1 libngtcp2-16
  libngtcp2-crypto-gnutls8 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-client
  xauth
Suggested packages:
  gettext-base git-doc git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn keychain libpam-ssh
  monkeysphere ssh-askpass
The following NEW packages will be installed:
  git git-man less libcbor0.10 libcurl3t64-gnutls libedit2 liberror-perl libfido2-1 libngtcp2-16
  libngtcp2-crypto-gnutls8 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 openssh-client
  xauth
0 upgraded, 19 newly installed, 0 to remove and 3 not upgraded.
Need to get 14.4 MB of archives.
After this operation, 64.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian trixie/main amd64 less amd64 668-1 [161 kB]
Get:2 http://deb.debian.org/debian trixie/main amd64 libedit2 amd64 3.1-20250104-1 [93.8 kB]
Get:3 http://deb.debian.org/debian trixie/main amd64 libcbor0.10 amd64 0.10.2-2 [28.3 kB]
Get:4 http://deb.debian.org/debian trixie/main amd64 libfido2-1 amd64 1.15.0-1+b1 [78.7 kB]
Get:5 http://deb.debian.org/debian-security trixie-security/main amd64 openssh-client amd64 1:10.0p1-7+deb13u2 [986 kB]
Get:6 http://deb.debian.org/debian-security trixie-security/main amd64 libngtcp2-16 amd64 1.11.0-1+deb13u1 [132 kB]
Get:7 http://deb.debian.org/debian-security trixie-security/main amd64 libngtcp2-crypto-gnutls8 amd64 1.11.0-1+deb13u1 [29.5 kB]
Get:8 http://deb.debian.org/debian trixie/main amd64 libcurl3t64-gnutls amd64 8.14.1-2+deb13u2 [383 kB]
Get:9 http://deb.debian.org/debian trixie/main amd64 liberror-perl all 0.17030-1 [26.9 kB]
Get:10 http://deb.debian.org/debian trixie/main amd64 git-man all 1:2.47.3-0+deb13u1 [2205 kB]
Get:11 http://deb.debian.org/debian trixie/main amd64 git amd64 1:2.47.3-0+deb13u1 [8862 kB]
Get:12 http://deb.debian.org/debian trixie/main amd64 libxau6 amd64 1:1.0.11-1 [20.4 kB]
Get:13 http://deb.debian.org/debian trixie/main amd64 libxdmcp6 amd64 1:1.1.5-1 [27.8 kB]
Get:14 http://deb.debian.org/debian trixie/main amd64 libxcb1 amd64 1.17.0-2+b1 [144 kB]
Get:15 http://deb.debian.org/debian trixie/main amd64 libx11-data all 2:1.8.12-1 [343 kB]
Get:16 http://deb.debian.org/debian trixie/main amd64 libx11-6 amd64 2:1.8.12-1 [815 kB]
Get:17 http://deb.debian.org/debian trixie/main amd64 libxext6 amd64 2:1.3.4-1+b3 [50.4 kB]
Get:18 http://deb.debian.org/debian trixie/main amd64 libxmuu1 amd64 2:1.1.3-3+b4 [21.9 kB]
Get:19 http://deb.debian.org/debian trixie/main amd64 xauth amd64 1:1.1.2-1.1 [35.9 kB]
Fetched 14.4 MB in 1s (10.4 MB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79,  line 19.)
debconf: falling back to frontend: Readline
Selecting previously unselected package less.
(Reading database ... 18182 files and directories currently installed.)
Preparing to unpack .../00-less_668-1_amd64.deb ...
Unpacking less (668-1) ...
Selecting previously unselected package libedit2:amd64.
Preparing to unpack .../01-libedit2_3.1-20250104-1_amd64.deb ...
Unpacking libedit2:amd64 (3.1-20250104-1) ...
Selecting previously unselected package libcbor0.10:amd64.
Preparing to unpack .../02-libcbor0.10_0.10.2-2_amd64.deb ...
Unpacking libcbor0.10:amd64 (0.10.2-2) ...
Selecting previously unselected package libfido2-1:amd64.
Preparing to unpack .../03-libfido2-1_1.15.0-1+b1_amd64.deb ...
Unpacking libfido2-1:amd64 (1.15.0-1+b1) ...
Selecting previously unselected package openssh-client.
Preparing to unpack .../04-openssh-client_1%3a10.0p1-7+deb13u2_amd64.deb ...
Unpacking openssh-client (1:10.0p1-7+deb13u2) ...
Selecting previously unselected package libngtcp2-16:amd64.
Preparing to unpack .../05-libngtcp2-16_1.11.0-1+deb13u1_amd64.deb ...
Unpacking libngtcp2-16:amd64 (1.11.0-1+deb13u1) ...
Selecting previously unselected package libngtcp2-crypto-gnutls8:amd64.
Preparing to unpack .../06-libngtcp2-crypto-gnutls8_1.11.0-1+deb13u1_amd64.deb ...
Unpacking libngtcp2-crypto-gnutls8:amd64 (1.11.0-1+deb13u1) ...
Selecting previously unselected package libcurl3t64-gnutls:amd64.
Preparing to unpack .../07-libcurl3t64-gnutls_8.14.1-2+deb13u2_amd64.deb ...
Unpacking libcurl3t64-gnutls:amd64 (8.14.1-2+deb13u2) ...
Selecting previously unselected package liberror-perl.
Preparing to unpack .../08-liberror-perl_0.17030-1_all.deb ...
Unpacking liberror-perl (0.17030-1) ...
Selecting previously unselected package git-man.
Preparing to unpack .../09-git-man_1%3a2.47.3-0+deb13u1_all.deb ...
Unpacking git-man (1:2.47.3-0+deb13u1) ...
Selecting previously unselected package git.
Preparing to unpack .../10-git_1%3a2.47.3-0+deb13u1_amd64.deb ...
Unpacking git (1:2.47.3-0+deb13u1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../11-libxau6_1%3a1.0.11-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.11-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../12-libxdmcp6_1%3a1.1.5-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.5-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../13-libxcb1_1.17.0-2+b1_amd64.deb ...
Unpacking libxcb1:amd64 (1.17.0-2+b1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../14-libx11-data_2%3a1.8.12-1_all.deb ...
Unpacking libx11-data (2:1.8.12-1) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../15-libx11-6_2%3a1.8.12-1_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.8.12-1) ...
Selecting previously unselected package libxext6:amd64.
Preparing to unpack .../16-libxext6_2%3a1.3.4-1+b3_amd64.deb ...
Unpacking libxext6:amd64 (2:1.3.4-1+b3) ...
Selecting previously unselected package libxmuu1:amd64.
Preparing to unpack .../17-libxmuu1_2%3a1.1.3-3+b4_amd64.deb ...
Unpacking libxmuu1:amd64 (2:1.1.3-3+b4) ...
Selecting previously unselected package xauth.
Preparing to unpack .../18-xauth_1%3a1.1.2-1.1_amd64.deb ...
Unpacking xauth (1:1.1.2-1.1) ...
Setting up libxau6:amd64 (1:1.0.11-1) ...
Setting up libxdmcp6:amd64 (1:1.1.5-1) ...
Setting up libxcb1:amd64 (1.17.0-2+b1) ...
Setting up libcbor0.10:amd64 (0.10.2-2) ...
Setting up libedit2:amd64 (3.1-20250104-1) ...
Setting up less (668-1) ...
Setting up liberror-perl (0.17030-1) ...
Setting up libx11-data (2:1.8.12-1) ...
Setting up git-man (1:2.47.3-0+deb13u1) ...
Setting up libx11-6:amd64 (2:1.8.12-1) ...
Setting up libngtcp2-16:amd64 (1.11.0-1+deb13u1) ...
Setting up libfido2-1:amd64 (1.15.0-1+b1) ...
Setting up libxmuu1:amd64 (2:1.1.3-3+b4) ...
Setting up libngtcp2-crypto-gnutls8:amd64 (1.11.0-1+deb13u1) ...
Setting up openssh-client (1:10.0p1-7+deb13u2) ...
Setting up libcurl3t64-gnutls:amd64 (8.14.1-2+deb13u2) ...
Setting up libxext6:amd64 (2:1.3.4-1+b3) ...
Setting up git (1:2.47.3-0+deb13u1) ...
Setting up xauth (1:1.1.2-1.1) ...
Processing triggers for libc-bin (2.41-12+deb13u2) ...
    
3. apt-get install unzip
実行結果:
root@05fa4e42bbcd:~# apt-get install unzip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  zip
The following NEW packages will be installed:
  unzip
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 173 kB of archives.
After this operation, 396 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian trixie/main amd64 unzip amd64 6.0-29 [173 kB]
Fetched 173 kB in 0s (1845 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79,  line 1.)
debconf: falling back to frontend: Readline
Selecting previously unselected package unzip.
(Reading database ... 19746 files and directories currently installed.)
Preparing to unpack .../unzip_6.0-29_amd64.deb ...
Unpacking unzip (6.0-29) ...
Setting up unzip (6.0-29) ...
    
4. apt-get install zip
実行結果:
root@05fa4e42bbcd:~# apt-get install zip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  zip
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 235 kB of archives.
After this operation, 642 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian trixie/main amd64 zip amd64 3.0-15 [235 kB]
Fetched 235 kB in 0s (2226 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79,  line 1.)
debconf: falling back to frontend: Readline
Selecting previously unselected package zip.
(Reading database ... 19764 files and directories currently installed.)
Preparing to unpack .../archives/zip_3.0-15_amd64.deb ...
Unpacking zip (3.0-15) ...
Setting up zip (3.0-15) ...
    
5. docker-php-ext-install zip
実行結果:→ 失敗
root@05fa4e42bbcd:~# docker-php-ext-install zip
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether cc accepts -g... yes
checking for cc option to enable C11 features... none needed
checking how to run the C preprocessor... cc -E
checking for egrep -e... (cached) /usr/bin/grep -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20230831
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for zip archive read/write support... yes, shared
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:

Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
    

libzip-devをインストール

1. apt-get install libzip-dev
事項結果:
root@05fa4e42bbcd:~# apt-get install libzip-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libzip5 zipcmp zipmerge ziptool zlib1g-dev
The following NEW packages will be installed:
  libzip-dev libzip5 zipcmp zipmerge ziptool zlib1g-dev
0 upgraded, 6 newly installed, 0 to remove and 3 not upgraded.
Need to get 1220 kB of archives.
After this operation, 2063 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian trixie/main amd64 libzip5 amd64 1.11.3-2 [62.6 kB]
Get:2 http://deb.debian.org/debian trixie/main amd64 zipcmp amd64 1.11.3-2 [19.8 kB]
Get:3 http://deb.debian.org/debian trixie/main amd64 zipmerge amd64 1.11.3-2 [14.5 kB]
Get:4 http://deb.debian.org/debian trixie/main amd64 ziptool amd64 1.11.3-2 [22.6 kB]
Get:5 http://deb.debian.org/debian trixie/main amd64 zlib1g-dev amd64 1:1.3.dfsg+really1.3.1-1+b1 [920 kB]
Get:6 http://deb.debian.org/debian trixie/main amd64 libzip-dev amd64 1.11.3-2 [181 kB]
Fetched 1220 kB in 0s (3538 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79,  line 6.)
debconf: falling back to frontend: Readline
Selecting previously unselected package libzip5:amd64.
(Reading database ... 19779 files and directories currently installed.)
Preparing to unpack .../0-libzip5_1.11.3-2_amd64.deb ...
Unpacking libzip5:amd64 (1.11.3-2) ...
Selecting previously unselected package zipcmp.
Preparing to unpack .../1-zipcmp_1.11.3-2_amd64.deb ...
Unpacking zipcmp (1.11.3-2) ...
Selecting previously unselected package zipmerge.
Preparing to unpack .../2-zipmerge_1.11.3-2_amd64.deb ...
Unpacking zipmerge (1.11.3-2) ...
Selecting previously unselected package ziptool.
Preparing to unpack .../3-ziptool_1.11.3-2_amd64.deb ...
Unpacking ziptool (1.11.3-2) ...
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack .../4-zlib1g-dev_1%3a1.3.dfsg+really1.3.1-1+b1_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.3.dfsg+really1.3.1-1+b1) ...
Selecting previously unselected package libzip-dev:amd64.
Preparing to unpack .../5-libzip-dev_1.11.3-2_amd64.deb ...
Unpacking libzip-dev:amd64 (1.11.3-2) ...
Setting up libzip5:amd64 (1.11.3-2) ...
Setting up zipmerge (1.11.3-2) ...
Setting up zlib1g-dev:amd64 (1:1.3.dfsg+really1.3.1-1+b1) ...
Setting up zipcmp (1.11.3-2) ...
Setting up ziptool (1.11.3-2) ...
Setting up libzip-dev:amd64 (1.11.3-2) ...
Processing triggers for libc-bin (2.41-12+deb13u2) ...
    

2. docker-php-ext-install zip
実行結果: 今度はOK!
root@05fa4e42bbcd:~# docker-php-ext-install zip
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether cc accepts -g... yes
checking for cc option to enable C11 features... none needed
checking how to run the C preprocessor... cc -E
checking for egrep -e... (cached) /usr/bin/grep -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20230831
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for zip archive read/write support... yes, shared
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:

Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
root@05fa4e42bbcd:~# apt-get install libzip-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libzip5 zipcmp zipmerge ziptool zlib1g-dev
The following NEW packages will be installed:
  libzip-dev libzip5 zipcmp zipmerge ziptool zlib1g-dev
0 upgraded, 6 newly installed, 0 to remove and 3 not upgraded.
Need to get 1220 kB of archives.
After this operation, 2063 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian trixie/main amd64 libzip5 amd64 1.11.3-2 [62.6 kB]
Get:2 http://deb.debian.org/debian trixie/main amd64 zipcmp amd64 1.11.3-2 [19.8 kB]
Get:3 http://deb.debian.org/debian trixie/main amd64 zipmerge amd64 1.11.3-2 [14.5 kB]
Get:4 http://deb.debian.org/debian trixie/main amd64 ziptool amd64 1.11.3-2 [22.6 kB]
Get:5 http://deb.debian.org/debian trixie/main amd64 zlib1g-dev amd64 1:1.3.dfsg+really1.3.1-1+b1 [920 kB]
Get:6 http://deb.debian.org/debian trixie/main amd64 libzip-dev amd64 1.11.3-2 [181 kB]
Fetched 1220 kB in 0s (3538 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 79,  line 6.)
debconf: falling back to frontend: Readline
Selecting previously unselected package libzip5:amd64.
(Reading database ... 19779 files and directories currently installed.)
Preparing to unpack .../0-libzip5_1.11.3-2_amd64.deb ...
Unpacking libzip5:amd64 (1.11.3-2) ...
Selecting previously unselected package zipcmp.
Preparing to unpack .../1-zipcmp_1.11.3-2_amd64.deb ...
Unpacking zipcmp (1.11.3-2) ...
Selecting previously unselected package zipmerge.
Preparing to unpack .../2-zipmerge_1.11.3-2_amd64.deb ...
Unpacking zipmerge (1.11.3-2) ...
Selecting previously unselected package ziptool.
Preparing to unpack .../3-ziptool_1.11.3-2_amd64.deb ...
Unpacking ziptool (1.11.3-2) ...
Selecting previously unselected package zlib1g-dev:amd64.
Preparing to unpack .../4-zlib1g-dev_1%3a1.3.dfsg+really1.3.1-1+b1_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.3.dfsg+really1.3.1-1+b1) ...
Selecting previously unselected package libzip-dev:amd64.
Preparing to unpack .../5-libzip-dev_1.11.3-2_amd64.deb ...
Unpacking libzip-dev:amd64 (1.11.3-2) ...
Setting up libzip5:amd64 (1.11.3-2) ...
Setting up zipmerge (1.11.3-2) ...
Setting up zlib1g-dev:amd64 (1:1.3.dfsg+really1.3.1-1+b1) ...
Setting up zipcmp (1.11.3-2) ...
Setting up ziptool (1.11.3-2) ...
Setting up libzip-dev:amd64 (1.11.3-2) ...
Processing triggers for libc-bin (2.41-12+deb13u2) ...
root@05fa4e42bbcd:~#
root@05fa4e42bbcd:~#
root@05fa4e42bbcd:~#
root@05fa4e42bbcd:~#
root@05fa4e42bbcd:~# docker-php-ext-install zip
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether cc accepts -g... yes
checking for cc option to enable C11 features... none needed
checking how to run the C preprocessor... cc -E
checking for egrep -e... (cached) /usr/bin/grep -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/local
checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20230831
checking for PHP installed headers prefix... /usr/local/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking for zip archive read/write support... yes, shared
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... yes
checking for zip_file_set_mtime in -lzip... yes
checking for zip_file_set_encryption in -lzip... yes
checking for zip_libzip_version in -lzip... yes
checking for zip_register_progress_callback_with_state in -lzip... yes
checking for zip_register_cancel_callback_with_state in -lzip... yes
checking for zip_compression_method_supported in -lzip... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... yes
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: patching config.h.in
configure: creating ./config.status
config.status: creating config.h
/bin/bash /usr/src/php/ext/zip/libtool --tag=CC --mode=compile cc -I. -I/usr/src/php/ext/zip -I/usr/src/php/ext/zip/include -I/usr/src/php/ext/zip/main -I/usr/src/php/ext/zip -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE    -DZEND_COMPILE_DL_EXT=1 -c /usr/src/php/ext/zip/php_zip.c -o php_zip.lo  -MMD -MF php_zip.dep -MT php_zip.lo
mkdir .libs
 cc -I. -I/usr/src/php/ext/zip -I/usr/src/php/ext/zip/include -I/usr/src/php/ext/zip/main -I/usr/src/php/ext/zip -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /usr/src/php/ext/zip/php_zip.c -MMD -MF php_zip.dep -MT php_zip.lo  -fPIC -DPIC -o .libs/php_zip.o
/bin/bash /usr/src/php/ext/zip/libtool --tag=CC --mode=compile cc -I. -I/usr/src/php/ext/zip -I/usr/src/php/ext/zip/include -I/usr/src/php/ext/zip/main -I/usr/src/php/ext/zip -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE    -DZEND_COMPILE_DL_EXT=1 -c /usr/src/php/ext/zip/zip_stream.c -o zip_stream.lo  -MMD -MF zip_stream.dep -MT zip_stream.lo
 cc -I. -I/usr/src/php/ext/zip -I/usr/src/php/ext/zip/include -I/usr/src/php/ext/zip/main -I/usr/src/php/ext/zip -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -DZEND_COMPILE_DL_EXT=1 -c /usr/src/php/ext/zip/zip_stream.c -MMD -MF zip_stream.dep -MT zip_stream.lo  -fPIC -DPIC -o .libs/zip_stream.o
/bin/bash /usr/src/php/ext/zip/libtool --tag=CC --mode=link cc -shared -I/usr/src/php/ext/zip/include -I/usr/src/php/ext/zip/main -I/usr/src/php/ext/zip -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H  -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE  -Wl,-O1 -pie  -o zip.la -export-dynamic -avoid-version -prefer-pic -module -rpath /usr/src/php/ext/zip/modules  php_zip.lo zip_stream.lo -lzip
cc -shared  .libs/php_zip.o .libs/zip_stream.o  -lzip  -Wl,-O1 -Wl,-soname -Wl,zip.so -o .libs/zip.so
creating zip.la
(cd .libs && rm -f zip.la && ln -s ../zip.la zip.la)
/bin/bash /usr/src/php/ext/zip/libtool --tag=CC --mode=install cp ./zip.la /usr/src/php/ext/zip/modules
cp ./.libs/zip.so /usr/src/php/ext/zip/modules/zip.so
cp ./.libs/zip.lai /usr/src/php/ext/zip/modules/zip.la
PATH="$PATH:/sbin" ldconfig -n /usr/src/php/ext/zip/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/src/php/ext/zip/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

+ strip --strip-all modules/zip.so
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
Xdebug: [Log Files] File '/tmp/xdebug.log' could not be opened.
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.25.0.1:9003 (through xdebug.client_host/xdebug.client_port).
Xdebug: [Log Files] File '/tmp/xdebug.log' could not be opened.
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.25.0.1:9003 (through xdebug.client_host/xdebug.client_port).
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o -o -name \*.dep | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la      modules/* libs/*
rm -f ext/opcache/jit/zend_jit_x86.c
rm -f ext/opcache/jit/zend_jit_arm64.c
rm -f ext/opcache/minilua      
    

Twigインストール

1. composer require twig/twig
実行結果:
※ composerコマンドはrootのhomeディレクトリで実行してしまったが、本来はPHPのプロジェクトフォルダ上で実行すること。
root@05fa4e42bbcd:~# composer require twig/twig
Xdebug: [Log Files] File '/tmp/xdebug.log' could not be opened.
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.25.0.1:9003 (through xdebug.client_host/xdebug.client_port).
./composer.json has been updated
Running composer update twig/twig
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Downloading symfony/polyfill-mbstring (v1.36.0)
  - Downloading symfony/polyfill-ctype (v1.36.0)
  - Downloading symfony/deprecation-contracts (v3.6.0)
  - Downloading twig/twig (v3.24.0)
  - Installing symfony/polyfill-mbstring (v1.36.0): Extracting archive
  - Installing symfony/polyfill-ctype (v1.36.0): Extracting archive
  - Installing symfony/deprecation-contracts (v3.6.0): Extracting archive
  - Installing twig/twig (v3.24.0): Extracting archive
Generating autoload files
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
Using version ^3.24 for twig/twig
    

まとめ

dockerコンテナの場合のインストール手順は・・
apt-get update && \
apt-get install -y \
libzip-dev \
git \
unzip \
zip \
&& docker-php-ext-install zip

composer require twig/twig


WSL Docker PHP Twig

PHP 学習:セッション管理を学ぶ


1. session_name(セッション名) でセッション名を設定
セッションIDを保持するクッキーの名前として使用。
設定しない場合はphp.ini session.name の値となる。

2. session_start関数
セッション管理機能を使うには、はじめに session_start関数を呼び出す
session_startを呼び出した後は $_SESSION変数にアクセスしてセッションデータの読み書きができるようになる。

3. session_regenerate_id関数
セッションIDを生成し直す。
ログイン成功時にセッションIDを変更すること。(セッションハイジャック対策)

4. クッキー削除
setcookie関数でクッキーの有効期限に過去日を設定することで削除する。

5. セッションデータ削除
$_SESSION.= array() でセッション変数のデータを削除
session_destroy関数でサーバーのセッションデータを削除

その他の記事