Support for PHP 5.4 built-in web server #634

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
4 participants
@sime
Contributor

sime commented May 5, 2012

Very basic Bash script to provide support for the PHP 5.4 built-in web server.

It assumes that App.baseUrl is set to env('SCRIPT_NAME') in core.php.

The ideal would be that CakePHP has/uses its own router.

More info: http://php.net/manual/en/features.commandline.webserver.php

@markstory

This comment has been minimized.

Show comment Hide comment
@markstory

markstory May 5, 2012

Owner

CakePHP does have and always uses the built-in router. What are you referring to?

Owner

markstory commented May 5, 2012

CakePHP does have and always uses the built-in router. What are you referring to?

@sime

This comment has been minimized.

Show comment Hide comment
@sime

sime May 6, 2012

Contributor

I believe with an appropriate router the prefix of /index.php/ wouldn't need to be in the URL. (I'm guessing)

Contributor

sime commented May 6, 2012

I believe with an appropriate router the prefix of /index.php/ wouldn't need to be in the URL. (I'm guessing)

@sime

This comment has been minimized.

Show comment Hide comment
@sime

sime May 7, 2012

Contributor

After a bit digging I'm lost as to how this would potentially use CakePHP's router.

Though having the ability to run ./Console/server to launch a server in the similar manner Rails projects can would be very handy.

Contributor

sime commented May 7, 2012

After a bit digging I'm lost as to how this would potentially use CakePHP's router.

Though having the ability to run ./Console/server to launch a server in the similar manner Rails projects can would be very handy.

@markstory

This comment has been minimized.

Show comment Hide comment
@markstory

markstory May 7, 2012

Owner

Perhaps, I'm confused by what you mean by router. I'm thinking of the Router class in CakePHP, which is always used. As for the pull request, could you also add the server script to the skel directory? Otherwise it won't be part of a newly created project.

We should probably also get a .bat for windows users, but I can probably take care of that.

Owner

markstory commented May 7, 2012

Perhaps, I'm confused by what you mean by router. I'm thinking of the Router class in CakePHP, which is always used. As for the pull request, could you also add the server script to the skel directory? Otherwise it won't be part of a newly created project.

We should probably also get a .bat for windows users, but I can probably take care of that.

@sime

This comment has been minimized.

Show comment Hide comment
@sime

sime May 7, 2012

Contributor

Sorry cannot help you with the Windows version.

Contributor

sime commented May 7, 2012

Sorry cannot help you with the Windows version.

@sime

This comment has been minimized.

Show comment Hide comment
@sime

sime May 28, 2012

Contributor

Spent a hour or so on a Windows machine trying to port the above to a Windows batch script. I couldn't work out how to provide the document root to PHP. Unfortunately there is no $APP equivalent.

Contributor

sime commented May 28, 2012

Spent a hour or so on a Windows machine trying to port the above to a Windows batch script. I couldn't work out how to provide the document root to PHP. Unfortunately there is no $APP equivalent.

@kamui545

This comment has been minimized.

Show comment Hide comment
@kamui545

kamui545 May 28, 2012

Contributor

Something like this should work:

@echo.
@echo off

SET lib=%~dp0

php -S localhost:8080 -t "%lib%/../webroot"

echo.
exit /B %ERRORLEVEL%
Contributor

kamui545 commented May 28, 2012

Something like this should work:

@echo.
@echo off

SET lib=%~dp0

php -S localhost:8080 -t "%lib%/../webroot"

echo.
exit /B %ERRORLEVEL%
@kamui545

This comment has been minimized.

Show comment Hide comment
@kamui545

kamui545 May 28, 2012

Contributor

The .bat file works for me.

But when we use the built-in web server, all URLs are prefixed.

Eg: When I request /pages/home, the URL to my CSS file is: /index.php/pages/css/cake.generic.css and when I request / the path is correct.

I tried to play with App.baseUrl, but no way to get it to work correctly

Contributor

kamui545 commented May 28, 2012

The .bat file works for me.

But when we use the built-in web server, all URLs are prefixed.

Eg: When I request /pages/home, the URL to my CSS file is: /index.php/pages/css/cake.generic.css and when I request / the path is correct.

I tried to play with App.baseUrl, but no way to get it to work correctly

@kamui545

This comment has been minimized.

Show comment Hide comment
@kamui545

kamui545 May 28, 2012

Contributor

It seems due to the fact that $this->request->webroot has for value /index.php/pages/ instead of /

Contributor

kamui545 commented May 28, 2012

It seems due to the fact that $this->request->webroot has for value /index.php/pages/ instead of /

@sime

This comment has been minimized.

Show comment Hide comment
@sime

sime May 28, 2012

Contributor

Yeah I'm getting the same problem. Can't put my finger on it.

Contributor

sime commented May 28, 2012

Yeah I'm getting the same problem. Can't put my finger on it.

@lorenzo

This comment has been minimized.

Show comment Hide comment
@lorenzo

lorenzo May 28, 2012

Owner

This is why I think PHP built-in server was worst idea ever :)

Owner

lorenzo commented May 28, 2012

This is why I think PHP built-in server was worst idea ever :)

@kamui545

This comment has been minimized.

Show comment Hide comment
@kamui545

kamui545 May 28, 2012

Contributor

Finally it works :)

/Console/server.bat

@echo.
@echo off

SET lib=%~dp0

php -S localhost:8080 -t "%lib%/../webroot" server_router.php

echo.
exit /B %ERRORLEVEL%

/Console/server_router.php

<?php
$ds = DIRECTORY_SEPARATOR;
$app = dirname(dirname(__FILE__));
$www_root = $app . $ds . 'webroot';

$url = parse_url($_SERVER['REQUEST_URI']);
$resource = $www_root . $ds . $url['path'];

if (file_exists($resource)) {
    return false; // serve the requested resource as-is.
} else {
    $_SERVER['PHP_SELF'] = 'index.php';
    require $www_root . $ds . 'index.php';
}
Contributor

kamui545 commented May 28, 2012

Finally it works :)

/Console/server.bat

@echo.
@echo off

SET lib=%~dp0

php -S localhost:8080 -t "%lib%/../webroot" server_router.php

echo.
exit /B %ERRORLEVEL%

/Console/server_router.php

<?php
$ds = DIRECTORY_SEPARATOR;
$app = dirname(dirname(__FILE__));
$www_root = $app . $ds . 'webroot';

$url = parse_url($_SERVER['REQUEST_URI']);
$resource = $www_root . $ds . $url['path'];

if (file_exists($resource)) {
    return false; // serve the requested resource as-is.
} else {
    $_SERVER['PHP_SELF'] = 'index.php';
    require $www_root . $ds . 'index.php';
}
@markstory

This comment has been minimized.

Show comment Hide comment
@markstory

markstory Jul 13, 2012

Owner

Closing in favor of #714 which seems to be a more maintainable way to provide the same feature.

Owner

markstory commented Jul 13, 2012

Closing in favor of #714 which seems to be a more maintainable way to provide the same feature.

@markstory markstory closed this Jul 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment