(PHP >= 5.3.0, PECL phar >= 2.0.0)
Phar::webPhar — mapPhar for web-based phars. front controller for web applications
Phar::mapPhar() for web-based phars. This method parses $_SERVER['REQUEST_URI'] and routes a request from a web browser to an internal file within the phar archive. In essence, it simulates a web server, routing requests to the correct file, echoing the correct headers and parsing PHP files as needed. This powerful method is part of what makes it easy to convert an existing PHP application into a phar archive. Combined with Phar::mungServer() and Phar::interceptFileFuncs(), any web application can be used unmodified from a phar archive.
Phar::webPhar() should only be called from the stub of a phar archive (see here for more information on what a stub is).
The alias that can be used in phar:// URLs to refer to this archive, rather than its full path.
The location within the phar of the directory index.
The location of the script to run when a file is not found. This script should output the proper HTTP 404 headers.
An array mapping additional file extensions to MIME type. By default, these extensions are mapped to these mime types:
<?php
$mimes = array(
'phps' => 2, // pass to highlight_file()
'c' => 'text/plain',
'cc' => 'text/plain',
'cpp' => 'text/plain',
'c++' => 'text/plain',
'dtd' => 'text/plain',
'h' => 'text/plain',
'log' => 'text/plain',
'rng' => 'text/plain',
'txt' => 'text/plain',
'xsd' => 'text/plain',
'php' => 1, // parse as PHP
'inc' => 1, // parse as PHP
'avi' => 'video/avi',
'bmp' => 'image/bmp',
'css' => 'text/css',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'htmls' => 'text/html',
'ico' => 'image/x-ico',
'jpe' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'js' => 'application/x-javascript',
'midi' => 'audio/midi',
'mid' => 'audio/midi',
'mod' => 'audio/mod',
'mov' => 'movie/quicktime',
'mp3' => 'audio/mp3',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'pdf' => 'application/pdf',
'png' => 'image/png',
'swf' => 'application/shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'wav' => 'audio/wav',
'xbm' => 'image/xbm',
'xml' => 'text/xml',
);
?>
An array mapping URI to internal file, simulating mod_rewrite of apache. For example:
<?php
array(
'myinfo' => 'myinfo.php'
);
?>
Es wird kein Wert zurückgegeben.
Throws PharException when unable to open the internal file to output, or if called from a non-stub. If an invalid array value is passed into mimetypes or to rewrites, then UnexpectedValueException is thrown.
Beispiel #1 A Phar::webPhar() example
With the example below, the created phar will display Hello World if one browses to /myphar.phar/index.php or to /myphar.phar, and will display the source of index.phps if one browses to /myphar.phar/index.phps.
<?php
// creating the phar archive:
try {
$phar = new Phar('myphar.phar');
$phar['index.php'] = '<?php echo "Hello World"; ?>';
$phar['index.phps'] = '<?php echo "Hello World"; ?>';
$phar->setStub('<?php
Phar::webPhar();
__HALT_COMPILER(); ?>');
} catch (Exception $e) {
// handle error here
}
?>
It appears that the $rewrites parameter is a callback not an array. The implementation seems to be more in tune with this note http://www.mail-archive.com/internals@lists.php.net/msg33627.html , although, I was unable to figure out when or if a parameter gets passed to that function. I implemented it as follows:
<?php
$phar_name = 'mysite.phar';
function phar_rewrites()
{
$r = $_SERVER['REQUEST_URI'];
if (file_exists("phar://$phar_name$r")) return $r;
else return 'index.php';
}
$phar = new Phar();
/*
* do stuff to set up phar
*/
$phar->webPhar($phar_name, 'index.php', '404.php', $mimes, 'phar_rewrites');
?>
The above implementation will also stop the phar from sending out a 301 redirect to the index file when you rewrite (in apache) all requests to the phar.