skip to Main Content

In CakePHP there is a built in web server that can be used for development.

This can be started with bin/cake server which then serves the application by default on http://localhost:8765

In my case I have an old CakePHP 3.8.13 application. On my MacBook the global php executable is PHP version 8:

% php -v
PHP 8.0.27 (cli) (built: Jan 12 2023 15:36:37) ( NTS )

This is problematic because CakePHP 3.8.13 is not designed to run on PHP 8. It works on PHP 7.

On my MacBook I have PHP versions 7.1, 7.2, 7.3 and 8.0 installed in /usr/local/opt/. These have been installed with brew. So if I want to execute PHP version 7.2 I can do it like this:

% /usr/local/opt/[email protected]/bin/php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

The question I have is: how can I make bin/cake server use a binary such as PHP 7.2, rather than the globally installed version PHP 8?

I tried alias php=/usr/local/opt/[email protected]/bin/php and this initially appears to work in that when subsequently executing php it uses the appropriate version:

% alias php=/usr/local/opt/[email protected]/bin/php
% php -v
PHP 7.2.34 (cli) (built: Jan 21 2023 06:18:05) ( NTS )

However, if I then stop and restart the inbuilt CakePHP web server it says the following in the logs:

% bin/cake server
built-in server is running in http://localhost:8765/
You can exit with `CTRL-C`
[Wed Apr 19 09:35:19 2023] PHP 8.0.27 Development Server (http://localhost:8765) started

Notice that this is using PHP 8.0.27 which isn’t what I want.

1

Answers


  1. The server command creates a new shell via PHP’s system() call, and aliases do not propagate into sub-shells.

    As of CakePHP 4.1.6 the server command recognizes the PHP environment variable, so that you could do something like:

    export PHP=/path/to/php ; bin/cake server
    

    With earlier versions you could either create your own CakePHP command that uses an environment variable, start the PHP built-in server manually (/path/to/php -S localhost:8765 -t /path/to/webroot/), or manage PHP versions with for example Homebrew as mentioned by @Cbroe in the comments.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search