5.5 KiB
5.5 KiB
Fix database connection and route navigation
Context
Two unrelated defects are breaking the app on Laragon (Windows, Apache, PHP, MySQL/MariaDB):
- Database connection fails.
config/database.phpreadsMYSQLHOST/MYSQLUSER/MYSQLPASSWORD/etc. viagetenv(). PHP under Apache/CGI on Laragon does not expose a project.envfile togetenv()automatically — only the real OS environment. There is novlucas/phpdotenvorapache_setenvcall. The.envfile at the project root is therefore ignored, sogetenv("MYSQLHOST")returnsfalse, and the PDO DSN is malformed (host=;dbname=unimarket;port=3306;…). - Broken navigation links.
index.phplinks to/public/customer/products/browse.phpand/public/owner/dashboard.php, but the actual folders live at/customer/…and/owner/…(nopublic/prefix).includes/sidebar.phpreferences aBASE_URLconstant that is never defined anywhere. Header/sidebar links will render as/owner/products/list.php(no host) or as the literal textBASE_URLowner/….
The .env file is otherwise correct: MYSQLHOST=127.0.0.1, MYSQLUSER=root, MYSQLPASSWORD= (empty), MYSQLPORT=3306, MYSQLDATABASE=unimarket. Database unimarket is created by unimarket.sql.
Decisions
- DB fix approach: Add a small inline parser for
.envinconfig/database.php(no new dependency). On first call it loads/path/to/project/.env, sets values into$_SERVER,$_ENV, andputenv(), then continues to usegetenv(). This is the minimum change that keepsgetenv()calls working everywhere else and avoids pulling in Composer. - Routes approach: Fix the offending links to use the real paths (
/customer/…,/owner/…,/admin/…) and defineBASE_URLonce. Do not rename folders or add apublic/rewrite — that conflicts with theguide.txtlayout. Unimarket/includes/sidebar.phpis a duplicate/stale file; we will not edit it (it is not referenced by any page). It is flagged in the risks so the team can delete it.
Tasks
1. config/database.php — load .env before reading env vars
- Add a function
loadEnv($path)that:- Returns silently if file missing or unreadable.
- Skips blank lines and
#comments. - For
KEY=valuelines, callsputenv("$KEY=$value"), sets$_ENV[$KEY], and$_SERVER[$KEY]. - Honors double-quoted values (strips quotes) and leaves empty values as
"".
- Call
loadEnv(__DIR__ . '/../.env')before thegetenv()reads. - Keep existing
try/catchso a real connection failure still shows the message.
2. index.php — fix two hero buttons
- Line 153:
/public/customer/products/browse.php→/customer/products/browse.php - Line 155:
/public/owner/dashboard.php→/owner/dashboard.php
3. includes/sidebar.php — define BASE_URL and fix trailing slashes
- At the top of the file (before any output), add a guard that defines
BASE_URLif not already set:if (!defined('BASE_URL')) { define('BASE_URL', '/'); } - Note: header.php links use absolute paths starting with
/already, so sidebar is the only place that needs the constant. The current three links render as/owner/products/list.phpetc., which is what we want. - (Optional but recommended) Remove the third link to
owner/reports.phpbecauseowner/reports.phpdoes not exist in the repo (see Risks). Decision: keep it for now — out of scope; this task is "fix what is broken", not "remove features".
4. Unimarket/includes/sidebar.php — flag as stale
- Do not edit. List in Open Questions so the team decides whether to delete it.
Files to change
config/database.php— addloadEnv()and call itindex.php— two href fixesincludes/sidebar.php— addBASE_URLguard
Out of scope
Unimarket/includes/sidebar.php(duplicate, not referenced anywhere)- Missing
owner/reports.php,owner/manage_orders.php,admin/reports.php— those are separate feature gaps, not routing bugs - Composer / phpdotenv adoption
- CSRF enforcement gaps and other security items
.htaccessorpublic/rewrite
Validation
Manual smoke test on Laragon (http://unimarket.test or http://localhost/Unimarket/):
- Hit
/index.php— page loads, noDatabase connection failedbanner. - Trending Products section renders up to 6 cards from the
productstable. - From a guest session, both header "Login"/"Register" links open the auth pages.
- Log in as an owner; click "Go to Dashboard" hero button — lands on
/owner/dashboard.php(not 404). - Log in as a customer; click "Browse Products" — lands on
/customer/products/browse.php. - From the owner sidebar, "Products" and "Orders" links open their list pages without "BASE_URL" appearing in the URL or the rendered HTML.
- Confirm by viewing source of any owner page that no literal string
BASE_URLappears inhrefattributes.
Risks / open questions
- Stale duplicate file:
Unimarket/includes/sidebar.phpmirrorsincludes/sidebar.phpand still usesBASE_URL. Recommend deleting the entireUnimarket/subfolder — confirm with team before doing so. - Empty
MYSQLPASSWORD: Laragon default MySQL root has no password; thegetenv("MYSQLPASSWORD") ?: ''works once.envis loaded. If a teammate's MySQL root does have a password, they must setMYSQLPASSWORD="theirpass"in.env. owner/reports.phpreferenced but absent: clicking the "Reports" sidebar link will 404. Flagged for the team; not fixed here.- No tests exist in the repo, so validation is manual only.