Na sigma máš vlastnú databázu MariaDB (názov = tvoj databázový login meno_priezvisko). PHP sa k nej pripája cez PDO (PHP Data Objects). Tento návod rieši, ako mať heslo mimo Git, ako sa z PHP pripojiť a ako pridať knižnice cez Composer.
Skúšaš vždy na sigma. Databáza je len tam a Kubeflow k nej priamo nevidí. Keďže každé uloženie ide na sigma samo, ladíš rovnako pohodlne, ako keby databáza bola vedľa teba.
On sigma you have your own MariaDB database (its name = your database login name_surname). PHP connects to it through PDO (PHP Data Objects). This guide covers keeping the password out of Git, connecting from PHP, and adding libraries with Composer.
You always test on sigma. The database exists only there, and Kubeflow cannot see it directly. Since every save goes to sigma by itself, debugging is as comfortable as if the database were next to you.
1. Heslo nepatrí do repository1. The password does not belong in the repository
Heslo k databáze sa nesmie dostať na GitHub, ani do súkromného repository. V každom projekte s databázou budú tri súbory:
| Súbor | Čo obsahuje | Je v Git? | Je na sigma? |
|---|---|---|---|
config.php |
skutočné údaje s heslom | nie (.gitignore) |
áno (SFTP ho nahrá pri uložení) |
config.example.php |
vzor bez hesla | áno | áno |
.gitignore |
zoznam, čo Git nesleduje | áno | áno |
Ak .gitignore v zadaní od učiteľa nie je, vytvor ho v koreni priečinka zadania:
config.php
vendor/
.vscode/
config.example.php:
<?php
return [
'host' => 'localhost',
'dbname' => 'meno_priezvisko',
'user' => 'meno_priezvisko',
'password' => 'DOPLNIT',
];
config.php: skopíruj config.example.php (pravý klik, Copy, pravý klik, Paste, premenuj) a doplň svoje údaje a heslo z cvičenia. Uložením sa dostane na sigma, do Git nikdy.
Overenie: po git push otvor repository na GitHub. config.php tam nesmie byť. Ak tam je, odstráň ho z repository: git rm --cached config.php, commit, push, a napíš učiteľovi.
The database password must never reach GitHub, not even a private repository. Every project with a database has three files:
| File | Contents | In Git? | On sigma? |
|---|---|---|---|
config.php |
the real credentials with the password | no (.gitignore) |
yes (SFTP uploads it on save) |
config.example.php |
a sample without the password | yes | yes |
.gitignore |
the list of what Git does not track | yes | yes |
If the teacher's assignment has no .gitignore, create it in the root of the assignment folder:
config.php
vendor/
.vscode/
config.example.php:
<?php
return [
'host' => 'localhost',
'dbname' => 'name_surname',
'user' => 'name_surname',
'password' => 'FILL_IN',
];
config.php: copy config.example.php (right click, Copy, right click, Paste, rename) and fill in your details and the password from the lab. Saving puts it on sigma, never into Git.
Check: after git push open the repository on GitHub. config.php must not be there. If it is, remove it from the repository: git rm --cached config.php, commit, push, and write to the teacher.
2. Pripojenie z PHP2. Connecting from PHP
db.php (v koreni zadania):
<?php
$cfg = require __DIR__ . '/config.php';
$pdo = new PDO(
"mysql:host={$cfg['host']};dbname={$cfg['dbname']};charset=utf8mb4",
$cfg['user'],
$cfg['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
V každom skripte potom require 'db.php'; a používaš $pdo. Skúška, test.php:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
require 'db.php';
$rows = $pdo->query('SHOW TABLES')->fetchAll();
echo 'Pripojené, tabuliek: ' . count($rows);
Otvor https://sigma.tuke.sk/student/meno.priezvisko/it/cv01/test.php. Ak vidíš „Pripojené“, funguje to. Ak vidíš chybu „Access denied“, skontroluj login a heslo v config.php.
Dáta a tabuľky si pozeráš a upravuješ v Adminer (návod 03 časť 4).
db.php (in the root of the assignment):
<?php
$cfg = require __DIR__ . '/config.php';
$pdo = new PDO(
"mysql:host={$cfg['host']};dbname={$cfg['dbname']};charset=utf8mb4",
$cfg['user'],
$cfg['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
In every script then require 'db.php'; and use $pdo. Test, test.php:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
require 'db.php';
$rows = $pdo->query('SHOW TABLES')->fetchAll();
echo 'Connected, tables: ' . count($rows);
Open https://sigma.tuke.sk/student/name.surname/it/cv01/test.php. If you see "Connected", it works. If you see "Access denied", check the login and password in config.php.
You look at and edit data and tables in Adminer (guide 03 part 4).
3. Composer: knižnice3. Composer: libraries
Composer spravuje PHP knižnice (Twig, PHPUnit…). Súbor composer.json hovorí, čo projekt potrebuje; composer.lock presné verzie; priečinok vendor/ obsahuje stiahnuté knižnice a nepatrí do Git (je v .gitignore).
Composer spúšťaš v Kubeflow, v termináli, v priečinku zadania (tu s knižnicou Twig ako príkladom):
cd ~/web/it/cv01
composer init --no-interaction
composer config platform.php 8.1.2
composer require twig/twig
platform.php 8.1.2 zabezpečí, že Composer vyberie verzie knižníc, ktoré bežia na sigma (PHP 8.1), aj keď v Kubeflow je 8.3.
Potom pravý klik na priečinok vendor v Explorer, Upload Folder. Priečinok vendor vznikol príkazom, nie uložením, preto ho treba nahrať ručne. To isté po každom ďalšom composer require.
V kóde: require __DIR__ . '/vendor/autoload.php'; a knižnice sú k dispozícii.
Ak si zadanie s knižnicami klonuješ nanovo (nový notebook, iný počítač), vendor v ňom nie je. Vytvoríš ho príkazom composer install (stiahne presne verzie z composer.lock) a nahráš cez Upload Folder.
Composer manages PHP libraries (Twig, PHPUnit…). The file composer.json says what the project needs; composer.lock the exact versions; the vendor/ folder holds the downloaded libraries and does not belong in Git (it is in .gitignore).
You run Composer in Kubeflow, in the terminal, in the assignment folder (here with the Twig library as an example):
cd ~/web/it/cv01
composer init --no-interaction
composer config platform.php 8.1.2
composer require twig/twig
platform.php 8.1.2 makes Composer pick library versions that run on sigma (PHP 8.1), even though Kubeflow has 8.3.
Then right click the vendor folder in Explorer, Upload Folder. The vendor folder was created by a command, not by saving, so it has to be uploaded by hand. The same after every further composer require.
In the code: require __DIR__ . '/vendor/autoload.php'; and the libraries are available.
If you clone an assignment with libraries afresh (a new notebook, another computer), vendor is not in it. You create it with composer install (downloads exactly the versions from composer.lock) and upload it with Upload Folder.
4. Kontrolný zoznam pred odovzdaním4. Checklist before submitting
config.phpnie je v repository (skontroluj na GitHub).vendor/nie je v repository.config.example.php,composer.json,composer.lock(ak používaš Composer) sú v repository.- Na sigma: stránka sa otvorí a pripojí sa k databáze.
config.phpis not in the repository (check on GitHub).vendor/is not in the repository.config.example.php,composer.json,composer.lock(if you use Composer) are in the repository.- On sigma: the page opens and connects to the database.
Keď to nejdeWhen it does not work
| Hláška alebo situácia | Čo skúsiť |
|---|---|
| Biela stránka | ini_set('display_errors', '1'); error_reporting(E_ALL); na začiatok skriptu |
SQLSTATE[HY000] [1045] Access denied |
zlý login alebo heslo v config.php na sigma; over v Adminer |
Unknown database |
dbname musí byť rovnaký ako login (meno_priezvisko) |
Failed to open stream: config.php |
config.php nie je na sigma; ulož ho v Kubeflow ešte raz (Ctrl + S) |
Class "Twig\…" not found |
vendor/ nie je na sigma; Upload Folder na vendor |
composer require hlási konflikt verzií s PHP |
chýba composer config platform.php 8.1.2, spusti ho a composer require znova |
config.php sa objavil na GitHub |
.gitignore chýbal alebo bol pridaný neskôr; git rm --cached config.php, commit, push, napíš učiteľovi |
| Message or situation | What to try |
|---|---|
| White page | ini_set('display_errors', '1'); error_reporting(E_ALL); at the top of the script |
SQLSTATE[HY000] [1045] Access denied |
wrong login or password in config.php on sigma; verify in Adminer |
Unknown database |
dbname must equal the login (name_surname) |
Failed to open stream: config.php |
config.php is not on sigma; save it in Kubeflow once more (Ctrl + S) |
Class "Twig\…" not found |
vendor/ is not on sigma; Upload Folder on vendor |
composer require reports a version conflict with PHP |
composer config platform.php 8.1.2 is missing; run it and composer require again |
config.php appeared on GitHub |
.gitignore was missing or added later; git rm --cached config.php, commit, push, write to the teacher |