I would try to solve this in PHP itself if it depended on me. Just create a .htaccess file that displays all possible requests in a single file (possibly index.php) and determines what to do next. This gives you the ability to do all kinds of bootstrapping and logging before delegating the request to any piece of code that should handle this request. You can even turn on and use a micro structure like Limonade to accomplish what you want. Here is an example:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d # if the requested directory does not exist, RewriteCond %{REQUEST_FILENAME} !-f # and the requested file does not exist, RewriteRule ^ index.php # map everything to index.php.
Then, in index.php, you can do all kinds of things to make sure you get the right answer. The easiest way to use a “structure-like controller” is to include an infrastructure like Limonade and use it. Example:
<?php require_once 'vendor/limonade.php'; dispatch( 'account/home', 'accountHome' ); function accountHome( ) { require_once 'modules/account/home.php'; } run( );
Obviously, this is just a suggestion. Alternatively, you can simply use an even simpler system, although I think you will have to write it yourself. So you can say that if a file exists in the modules directory, just include this file and that.
<?php $path = isset( $_SERVER['PATH_INFO'] ) ? trim( $_SERVER['PATH_INFO'], '/' ) : null; if( $path !== null ) { $filename = 'module/' . $path . '.php'; if( file_exists( $filename ) ) { require_once $filename; } else { require_once 'error.php'; } } else { require_once 'home.php'; }
What is it. Fully functional and all. You could use a library that sorts all this for you.
source share