Struggling with MVC in e-commerce project

I’m building an online store for my school assignment using MVC for the first time. Most things are working fine but I’m having trouble showing products and users from the database. Here’s what I’ve got so far:

  1. In index.php, I set up the session and database connection.
  2. UserRepository.php handles database queries.
  3. UserService.php acts as a middleman between the repository and controller.
  4. The controller has a usersView() function to fetch and display users.
  5. The view file should show a table of users.

I’ve checked that the database queries work when run directly. But when I try to show the users in the view, it’s coming up empty. I used count($users) to debug and it returned null.

Am I missing something in how MVC connects these parts? Any ideas on why the users aren’t showing up in the view?

// Example of what I expected to work but doesn't
function usersView() {
    $users = $this->userService->getAll();
    require 'views/admin/users.php';
}

I’d really appreciate any help figuring out where the data is getting lost between the database and the view. Thanks!

I’ve encountered similar challenges with MVC in e-commerce projects. One often overlooked issue is dependency injection. Are you properly injecting the UserService into your controller? If not, the $this->userService might be null.

Another potential problem could be in your database connection. Ensure it’s established correctly in index.php and persists throughout the request lifecycle. Sometimes, the connection can close prematurely.

Have you implemented error handling in your UserRepository? It’s possible an exception is being thrown but not caught, causing silent failures. Try wrapping your database queries in try-catch blocks and log any errors.

Lastly, check your UserService’s getAll() method. Make sure it’s actually calling the repository method and returning the result. A common mistake is forgetting to return the data from the service layer.

These are just a few possibilities to investigate. Good luck troubleshooting!

Hey BoldPainter45! Your MVC setup sounds pretty solid so far. Have you considered that the issue might be in how you’re calling the controller method? Sometimes the routing can trip things up.

Maybe try adding a var_dump or print_r in your controller just before you require the view file? Something like:

function usersView() {
    $users = $this->userService->getAll();
    var_dump($users);
    require 'views/admin/users.php';
}

This way you can see what’s actually in $users when it hits the controller. If it’s empty there, the problem’s likely in your service or repository. If it has data, then the view might not be using it correctly.

Also, just curious - are you using any specific framework or is this a custom MVC setup? Some frameworks have quirks in how they handle data passing that might be worth looking into.

Let us know what you find out! It’s always interesting to see how these tricky issues get resolved.

hey there, i’ve run into similar issues before. sounds like ur data isnt being passed correctly between layers. have u tried debugging in the service layer to see if getAll() is returning data? also, make sure ur view is actually using the $users variable. sometimes its easy to forget to echo the data in the view file. hope that helps!