0

I try to build a little router system in my project and everything should work. but i get this error:

Warning: require_once(../view/homeView.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\MyShop\MyShop\MVC\controller\homeController.php on line 3

I dont't know what to do because the homeView.php is in /MVC/view/homeView.php.

this are the files:

index.php

<?php

$request = $_SERVER['REQUEST_URI'];

switch ($request)
{
    case "/MyShop/MyShop/":
    case "/MyShop/MyShop/home":
        require __DIR__ . "/MVC/controller/homeController.php";
        break;

    case "/MyShop/MyShop/shop":
        require __DIR__ . "/MVC/controller/produkteController.php";
        break;

    case "/MyShop/MyShop/profil":
        require __DIR__ . "/MVC/controller/profilController.php";
        break;

    case "/MyShop/MyShop/registrieren":
        require __DIR__ . "/MVC/Controller/registrierenController.php";
        break;
    default:
        echo "seite nicht gefunden";

}

homeController.php

<?php

require_once "../view/homeView.php";
require_once "../view/grundseiteView.php";

the directory structure looks like this

∟MyShop
 -index.php
 ∟MVC
  ∟controller
   -homeController.php
  ∟view
   -homeView.php
   -grundseiteView.php
 

If I put the view files in the controller directory, it works. But I don't know why it doesn't work now. maybe someone can help me

anyone
  • 39
  • 4
  • use require_once __DIR__ . "/relative/path/from/current/file". The __DIR__ magic constant(https://www.php.net/manual/en/language.constants.predefined.php) returns the directory of the current file – Arijeet Bose Feb 01 '22 at 17:06
  • check directory and file permissions and try require_once __DIR__ . "/MVC/view/homeView.php"; – ShapCyber Feb 01 '22 at 17:10
  • Relative paths are _relative to your "entry point" file_, ie. ```index.php```, not to each file included down the road. If your ```index.php``` does ```require_once 'foo/bar/req.php```, then ```req.php``` is "level" with ```index.php```... Please see [Are PHP include paths relative to the file or the calling code?](https://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code) – Markus AO Feb 01 '22 at 17:50
  • Does this answer your question? [Are PHP include paths relative to the file or the calling code?](https://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code) – Markus AO Feb 01 '22 at 17:51

0 Answers0