Home » Php » php – router – move all files in public folder

php – router – move all files in public folder

Posted by: admin February 25, 2020 Leave a comment


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.


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


$request = $_SERVER['REQUEST_URI'];

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


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

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


<a href='account.php?action=edit_settings&amp;do=edit'>edit</a>
How to&Answers:

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(
    \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME |

$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)) {


This is an idea of what you could do.