r/PHPhelp • u/Defiant-Ad3530 • Jun 11 '25
Anyone can help me with PHP routing, using MVC architecture?
edit : just fixed it!! thank you for all your help :))
Hello, so im creating a budget travel planner system using PHP PDO, in mvc architecture form. I've almost finished most of the functions and have been testing with dummy views but now I recieved the frontend from my group member and need it to link it. However, im really struggling with that and the routing part, so can anyone help me with this, please?
for example, this is my user controller :
<?php
session_start();
require __DIR__ . '/../models/User.php';
include_once __DIR__ . '/../helpers/session_helper.php';
class UserController {
private $userModel;
public function __construct(){
$this->userModel = new User;
// $this->userModel = new User($pdo);
}
// register user
public function registerUser(){
// if register button was clicked in the form // LOOK
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['registerUser']))
{
// lets sanitize the data to remove any unneccesary data
$firstName = filter_var(trim($_POST['firstName']), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$lastName = filter_var(trim($_POST['lastName']), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$contactNumber = filter_var(trim($_POST['contactNumber']), FILTER_SANITIZE_NUMBER_INT);
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
$password = filter_var(trim($_POST['password']));
// $confirmPassword = trim($_POST['confirmPassword']);
// if ($password !== $confirmPassword) {
// flash("register", "Passwords do not match.");
// redirect("../signup.php");
// }
// initalize data
$data = [
'name' => $firstName . ' ' . $lastName,
'contact_number' => $contactNumber,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'role' => 'user'
];
// validate the inputs before saving
if($this-> userModel-> registerUser($data)){
flash("register", "The account was created sucessfully!");
}else{
die("Something went wrong");
}
}
}and this is my index php file for the routing:
<?php
require_once __DIR__ . '/Connection.php';
require_once __DIR__ . '/controllers/UserController.php';
$pdo = new Connection;
// $pdo = (new Connection())->pdo;
$controller = new UserController;
// routing for user registration
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['registerUser']))
{
$controller ->registerUser();
}
else {
echo "Error";
}
?>However, it doesnt work and user does not get registered, which is weird because it worked when i called the CONTROLLER in the view, instead of using the routing method. I don't understand what's going wrong.
I only have about 4 days left until we need to run it and do the testing ðŸ˜ðŸ˜ thank you!
Anyone can help me with PHP routing, using MVC architecture?
2
u/MateusAzevedo Jun 11 '25
Things that will help:
Enabling full error reporting, to see all errors reported by PHP;
Debugging. If your code doesn't produce any error but still doesn't do what you expect, you have a logic problem. Only debugging can help in this case.
Since you confirmed the controller method works (when you called it directly), then it's very likely the issue is in the routing logic. Start with a simple
var_dump($_POST);to see what PHP received from the frontend.1
u/equilni Jun 12 '25
I see this was already solved. Just a few tips:
a) I would recommend setting up an autoloader for your classes. This would remove the include/requires.
I take it this is a class project, if you haven't learned about namespaces and PSR-4 (and/or Composer, if you are allowed to use libraries), I would suggest looking into that.
b) I don't see this as part of the code or second part of the question:
I recieved the frontend from my group member and need it to link it. However, im really struggling with thatAt basics this looks like:
The template part could look like (which functions similarly to Plates):
Escape output in the template using htmlspecialchars
Further reading: https://phptherightway.com/#templating
c) Your routing could be better by routing via HTTP Method & url
Consider HTTP at first:
Expand this to pseudo PHP:
Query strings:
Clean urls:
Now, you've removed the
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['registerUser']))lines.registerUserwon't run unless it's called by the URL via POST.$_POST['registerUser']isn't tied to the template (unless you provide this in the View).d) Last tip would be to consider using Dependency Injection. Essentially, it's passing the created objects to the classes that depend on it, typically via the constructor:
Reading: https://phptherightway.com/#dependency_injection
1
u/Hopeful_Ad6629 Jun 13 '25
Is this homework? It sounds like homework!
0
u/Primary_Garlic4253 Jun 11 '25
How does the frontend part looks like?
I can only guess that you're using forms and I think you can resolve it like this:
`
<form action="index.php" method="POST">
<input type="text" name="firstName">
<input type="text" name="lastName">
...
<input type="hidden" name="registerUser" value="1">
<button type="submit">Register</button>
</form>\`