0

I'm trying to implement routes in php.

.htaccess is configured.

The following works at the base directory in the routes.php file.

 $request = $_SERVER['REQUEST_URI'];
 $version = '2_0_2';

 switch ($request) {
     case '/' :
         require __DIR__ . '/bonify'. $version . '/app/index.php';
         break;
     case '' :
         require __DIR__ . '/bonify'. $version . '/app/index.php';
         break;
     case '/bonify' :
         require __DIR__ . '/app/index.php';
         break;
     default:
         http_response_code(404);
         require __DIR__ . '/views/404.php';
         break;
       }
 

However, when the /bonify2_0_2/app/index.php file is accessed from the url root it wants to include the routes.php file.

I try to include in the /bonify2_0_2/app/index.php file include('route.php'); this puts it into an infinite loop.

I try to include in the /bonify2_0_2/app/index.php file include('header.php'); which is located in /bonify2_0_2/app/ and it works.

I try to include in the /bonify2_0_2/app/index.php file include('../../../route.php'); and it failed to open stream:.

Clearly I'm not quite understanding how routes work, I assume everything is called/served from the url route directory and everything is relative to that?

Thanks for pointers.

Spatial Digger
  • 1,883
  • 1
  • 19
  • 37
  • 2
    The infinite loop has nothing to do with 'routes' specifically. You've created a circular reference by including `index.php` from a file included _by_ `index.php`. This probably needs a redesign. However if those rewrites are all you're doing, then why not delegate this to mod_rewrite and be done with it? – Ro Achterberg Dec 12 '20 at 11:19
  • Just a quick side note: no need to duplicate code for the cases. You can remove the body of `case '/' :` completely and it will still work - when you make an empty case, it will fall through to the one below it. – El_Vanja Dec 12 '20 at 11:20
  • @RoAchterberg ok, so I'll change the second index.php to something sensible. – Spatial Digger Dec 12 '20 at 11:24
  • @El_Vanja thanks, I read about this too, but just keeping it like this for now while I get it working. – Spatial Digger Dec 12 '20 at 11:24
  • @RoAchterberg ok, so I fixed the `index.php` issue renaming the file to `bonify.php`. Now I get this issue where it is not including the `route.php` file: `Warning: include(../../../route.php): failed to open stream: No such file or directory in ` – Spatial Digger Dec 12 '20 at 18:21
  • 1
    Sounds like you need to anchor your relative paths. See [this answer](https://stackoverflow.com/questions/17407664/php-include-relative-path) for starters. – Ro Achterberg Dec 12 '20 at 19:08
  • @RoAchterberg ok, so as you say I need to anchor the relative paths. I've tried adding `define('ROOT_PATH', dirname(__DIR__) . '/');` to the now named `bonify.php` but that sets the path of the bonify.php. I've tried `include(dirname(__DIR__).'../../../routes.php');` but that just appends `../../../routes.php` to the directory path. – Spatial Digger Dec 12 '20 at 20:20
  • Try it without the `dirname()`. – Ro Achterberg Dec 12 '20 at 20:24
  • @RoAchterberg without the `dirname()`, `include((__DIR__).'../../../routes.php');` If I echo the result it gives : `... xampp\htdocs\bonify\bonify2_0_2\app../../../routes.php` – Spatial Digger Dec 12 '20 at 20:33
  • I assumed some basic knowledge of how directories work. The result you `echo`ed should have probably prompted you to add the slash and to also normalize the separators to all backslashes, as it appears you're on a Windows system. So replace the slashes with backslashes and prefix your relative path with a backslash. You don't need to enclose `__DIR__` in parentheses. – Ro Achterberg Dec 12 '20 at 21:36

0 Answers0