As per title, I created a very simple test phar. I'd like to test it with the built-in webserver(PHP 5.4) but it seems impossible.
php -S localhost:80 /path/to/myphar.php
Result: blank page (in the phar front controller is index.php
doing some HTML output).
php -S localhost:80 -t /path/to/folder_with_index_and_phar
With loader script:
<?php
require 'phar://myphar.php'
Result in a blank page, again.
Is this even possible or I have to use Apache?
EDIT: after an year I need this again. Here is what's happening (from answers):
php -S localhost:80 -t /path/to/app.phar
php -S localhost:80 -t /path/to/app.phar
Results in an error: /path/to/app.phar is not a directory.
While:
php -S localhost:80 -t .
http://localost/app.phar/foo/bar
Works, but I don't need the app.phar "prefix"!
Using Apache and setting the directory index to app.phar works as expected.
php -S localhost:80 -t /path/to/phar
isn't going to work since the -t
argument is meant to be used for directories. The trick is that the php webserver supports passing a router as argument. Therefore, suppose your phar is in /tmp (it can be anywhere), this is how you create a simple router, and setup the webserver:
echo '<?php require("/path/to/phar");'>/tmp/router.php
php -S localhost:80 /tmp/router.php
php -S localhost:8080 phpinfo.phar
That seemed to work for me just fine from a simple phar file I created to out put phpinfo();
I did notice in the PHP docs, when creating the PHAR file, that you seem to need to setup stubs for CLI and WWW interfaces.
http://php.net/manual/en/phar.buildfromdirectory.php
<?php
// create with alias "project.phar"
$phar = new Phar('project.phar', 0, 'project.phar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
$phar->setStub($phar->createDefaultStub('cli/index.php', 'www/index.php'));
EDIT
Specifically, this is what I did.
#!/usr/bin/env php
<?php
$phar = new Phar('phpinfo.phar', 0, 'phpinfo.phar');
$phar->buildFromDirectory(__DIR__ . '/phpinfo');
$phar->setStub($phar->createDefaultStub('index.php'));
to create a phar file of a directory:
phpinfo/
└── index.php
and index.php was just:
<?php phpinfo();
After hours of messing around with built-in web server, finally found the solution to what you are looking for. The key is router file for the web server. Follow below procedure
1) Create a router.php file for built-in web server with following content
$path = $_SERVER["REQUEST_URI"];
if($pathPosition = strpos($path,'/myapp') !== false){
//Request has been made to Phar-ed application
$pathPosition += strlen('/myapp');
$file = substr($path, $pathPosition);
//Here include the base path of your Phar-ed application with file path
//I placed myphar.phar and router.php in the root directory for this example
require_once("phar://myphar.phar/$file");
exit;
}
return FALSE;
What this does: It checks the Uri for your application name, in my case "/myapp" and then extract the file path you want to access. Then includes the desired phared file into current script and exits. Else process the request as normal.
2) Start the built-in web server as follows:
php -S localhost:80 -t . router.php
What this does: Fires up the built-in webserver making current directory as document root folder and uses router.php as router file (Placed in the same folder)
3) Make a request in browser like this:
http://localhost/myapp/foo/bar
You will get the Phar-ed file from you application .phar file
Let me know in case of questions.
Note: You can also use regex to match the Uri pattern in router.php
So, I do not understand why you need the Apache server. After the command
php -S localhost:80 -t /path/to/myphar.php
PHP 5.5.9-1ubuntu4 Development Server started at Fri Jun 6 22:27:49 2014
Listening on http://localhost:80
Document root is /tmp/http
Press Ctrl-C to quit.
means all is well, you go to your browser's address bar write
http://localhost:80
and see the result of the script execution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With