skip to Main Content

This is my first time using PHP/MVC architecture and

The problem is an error 404. I’m using MVC architecture with php.

The process should be working like this:
1.The Router gets the current URL and the method used with

$_SERVER['REQUEST_URI'] 
$_SERVER['REQUEST_METHOD']

2.The router gets an specific function for the current URL. Said function is defined on index.php in PUBLIC for ever URL of the project
3.The router calls the function. The functions are in the Controller.
call_user_func

This is only working for the Main Url (defined as ‘/’ in the index.php from PUBLIC).
Whenever I want to access any other URL I get the 404 error. It’s like if the server couldn’t access the folder with the VIEWS.

But I know it works because the VIEW index.php is rendering the URL ‘/’. I tried exchanging the functions called by the URL’s to see if there was something wrong with the Classes or anything else, but it also worked (example: I called the function "contact" from the URL ‘/’ instead of the function "index" and I got the correct ‘/contact’ VIEW)

I’m sure my project is working on my localhost.
I even deployed it on another CPanel and it work correctly, the difference is that the other is a VPS and the one with the problem is a shared hosting.

From what I can understand is that the issue should be one of the following:
-the htaccess file
-the php ini
-the composer autoload is not working

My php version is correct and my database is working properly.

Was I able to explain my problem? Send Help Please!

Here’s my code for the Router and index.php on PUBLIC

router
index.php

2

Answers


  1. Chosen as BEST ANSWER

    Nevermind I solved it If out there's any noob like me witht the same problem... I solved with the htaccess file

    it should be like this

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    And it should be in the same folder as your index.php (PUBLIC folder)


  2. I’m facing the same problem but wasn’t able to fix it. I can’t access other files. Here is my project dir

    project/
    ├── public/
    │   ├── .htaccess
    │   ├── index.php
    │   └── ... (other public files)
    ├── views/
    │   └── error_404.php
    │   └── ... (other files)
    ├── controllers/
    │   └── ... (controller files)
    ├── models/
    │   └── ... (model files)
    └── router.php
    

    here is my htaccess file:

    Options -Indexes
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes If Not A Folder...
     
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
    
        ErrorDocument 404 ../views/error_404.php
    </IfModule>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search