Bootstrap PHP code
Simple code to funnel the HTTP requests in one single file, which handles the web application.
Usage
How to use it:
1. On your home directory create a folder (name it "site_src") to put the application's files inside it.
2. Inside the directory for the classes create a php code file and name it "index.php".
3. Paste the code below onto the file you have just created:
class index
{
public function __construct()
{
}
public function index($args)
{
echo 'This is the index page, shown by default to all requests that cannot be routed';
}
}
4. Again create another class file in the same directory and name it "welcome.php"
5. Paste the code below onto the file:
class welcome
{
public function __construct()
{
}
public function index($args)
{
// redirects to test()
$this->test($args);
}
public function test($args)
{
if (isset($args[0])) echo $args[0];
if (isset($args[1])) echo ' '.$args[1];
}
}
6. Add this rewrite code to your .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_/\ ] )$ index.php?route=$1 [L,QSA]
Note that this rewrite allows only specific characters in the URI.
7. Grab the code from snippet and paste it in the index file (index.php) inside your public_html directory.
8. Test the bootstrap code by surfing to your site: http://www.example.com/welcome/test/hello/steve/
Comments
Nope: Shorttags will stay forever.
ellisgl, wiki said about PHP6:
New features: traits, array dereferencing, closure $this support, JsonSerializable interface, "<?=" no longer requires 'short_open_tag' set to ON
http://en.wikipedia.org/wiki/PHP
I don`t know why it written there but I can imagine the reason. I think it's not a fake. ;-)
Is this still active
Nice
ellisql: PHP6 will still allow <? short tags. Support for ASP ones
Hi denki,
of course you can. You can use a template class to pass variable so they will
be available to the "html" php page.
The simplest working template class for this code would be:
Where "output" is the folder with the template. So in the welcome.php class for
example you can instantiate the template class, run the page code and then
assign variables to the template file:
And the "html" (template) php page will have something like this:
<?=$arg1;?>
<?=$arg2;?>
If the paths are fine doing it like this it will work.
Hi,
I wonder if it's possible to use variables inside the application's files (welcome.php). And have them interpreted in the main file.
I want to change a page title tag with any new URL for example. Or is "echo" the only way to generate output?
Thanks
Denki
Yes, spl_autoload would be good if you are mixing libraries.
Here's my refactoring..
Also I would use spl_autoload_register instead of the __autoload directive
Note: Stop using short tags. Turn short tags off in your ini file. PHP 6 will no longer support it, also if you need to deal with XML documents, you have to do thing to prevent the PHP engine from trying to parse it as a PHP file.