0

I wish to move all files out of public folder except "index.php" & "assets." I'm slowly updating to a custom MVC for my small project: I've moved functions, config & classes etc. - it's just the old spaghetti PHP files left. I want to move them all before I start breaking them down into models, views etc.

I would prefer core PHP and not to use symfony if any one can help.

So, instead of all files been on same level as "index.php" everything would route to new folder with all the files and new "index2.php" - so I've used a simple way that works on some level but there are 30 files, and I do not wish to write each one out if there's a better way.

htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]

index.php:

<?php
$request = $_SERVER['REQUEST_URI'];

switch ($request) {
    case '/' :
        require __DIR__ . '/app/memberlist.php';
        break;
    case '' :
        require __DIR__ . '/app/memberlist.php';
        break;
    case '/staff' :
        require __DIR__ . '/app/staff.php';
        break;
    default:
        http_response_code(404);
        require __DIR__ . 'app/pdointro.php';
        break;

}

Also there are some paths in the file I don't know how to add like:

header("Location: account-details.php?id=$id"); 

and:

<a href='account.php?action=edit_settings&amp;do=edit'>edit</a>
tereško
  • 58,060
  • 25
  • 98
  • 150
M-jay
  • 21
  • 2

1 Answers1

0

Nice you choose front controller design pattern. I think you could create an array with all your php files dynamically and then make a comparison with REQUEST_URI.

Take a look at php docs:

Example below :

$files = [];

$dirIterator = new \RecursiveDirectoryIterator(
    'your_php_project_root_dir',
    \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME |
    \FilesystemIterator::CURRENT_AS_SELF
);

$fileIterator = new \RecursiveIteratorIterator($dirIterator);

foreach($fileIterator as $file) {
    $filePath = $file->getPathName();
    $fileExt = $file->getExtension();
    $fileName = $file->getFileName();
    $fileSubPath = $file->getSubPath();
    $isFile = $file->isFile();

    // here you can filter, make condition and then add php files to your array.
    // $files[$fileName] 
}

if ($path = \array_search($_SERVER['REQUEST_URI'], $files)) {
    require($path);
}

exit;

This is an idea of what you could do.

Lounis
  • 597
  • 7
  • 15