Question

error 500.autoload problem PHP-MVC

Posted May 21, 2020 638 views
PHP

hi, im trying to deploy my php-mvc app and i cant get my autoload.php to work.
Everything works correctly on localhost, but when deploying on the server it gives me 500 (Internal error server) … I have tried to edit the apache files, activate mod_rewrite and leave everything configured to have friendly routes and still get the same problem. Apparently the code of my index.php is not working.
Can anyone help?
Thanks!
sorry for my english

I attach my code and the apache error log:

autoload.php

<?php

function controllers_autoload($classname){
    include 'controllers/' . $classname . '.php';   
}

spl_autoload_register('controllers_autoload');

index.php

function show_error(){
    $error = new errorController();
    $error->index();
}

if(isset($_GET['controller'])){
    $nombre_controlador = $_GET['controller'].'Controller';
}elseif(!isset($_GET['controller']) && !isset($_GET['action'])){
    $nombre_controlador = controller_default;
}else{
    show_error();
    exit();
}

if(class_exists($nombre_controlador)){
    $controlador = new $nombre_controlador();

    if(isset($_GET['action']) && method_exists($controlador, $_GET['action'])){
        $action = $_GET['action'];
        $controlador->$action();
    }elseif(!isset($_GET['controller']) && !isset($_GET['action'])){
        $action_default = action_default;
        $controlador->$action_default();
    }else{
        show_error();
    }
}else{
    show_error();
}

.htaccess

<IfModule mod_rewrite.c>
# Activar rewrite
RewriteEngine on
ErrorDocument 404 http://ip/psweb/error/

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(.*)/(.*) index.php?controller=$1&action=$2

# DirectoryIndex index.php
</IfModule>

error.log

PHP Warning:  include(controllers/homeController.php): failed to open stream: No such file or directory in /var/www/html/psweb/autoload.php on line 4

PHP Warning:  include(): Failed opening 'controllers/homeController.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/psweb/autoload.php on line 4

PHP Warning:  include(controllers/errorController.php): failed to open stream: No such file or directory in /var/www/html/psweb/autoload.php on line 4

PHP Warning:  include(): Failed opening 'controllers/errorController.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/psweb/autoload.php on line 4

PHP Fatal error:  Uncaught Error: Class 'errorController' not found in /var/www/html/psweb/index.php:18\nStack trace:\n#0 /var/www/html/psweb/index.php(46): show_error()\n#1 {main}\n  thrown in /var/www/html/psweb/index.php on line 18
edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

the error was that linux is case sensitive xD

Hi @FLP,

Looking at the errors you’ve provided, it seems like the following is the actual problem :

PHP Fatal error:  Uncaught Error: Class 'errorController' not found in /var/www/html/psweb/index.php:18\nStack trace:\n#0 /var/www/html/psweb/index.php(46): show_error()\n#1 {main}\n  thrown in /var/www/html/psweb/index.php on line 18

It seems like it can’t find the errorController(); class. It’s possible somewhere in your code you are not extending it. I’ll recommend checking where exactly are you including the file which holds the said class.

Regards,
KDSys

  • Thanks for your answer @KDSys , i verified the location of my files and everything is in order.
    I tried to add my two controllers to my index.php file as follows:

    require_once 'autoload.php';
    require_once 'controllers/errorController.php';
    require_once 'controllers/homeController.php';
    

    And so I could see my website correctly, which leads me to deduce that the server is not correctly taking my autoload.php…

    Will it be an include () problem?

    Regards

    • Hmm, most probably the server can’t see it properly due to some misconfiguration. It does seem like a problem with the paths in your website so maybe you should start from there. It’s kinda strange how it sees the file but not the function itself.

      Anyway, this shouldn’t be related to the server but to your application. In my experience, it was always the case

      • Thanks anyway for your help @KDSys, I have looked for the possible error for my code a week ago and I am not getting a solution…
        Will it be any difference from Linux and Windows?

Submit an Answer