I recently made a contribution to Laravel Valet which adds the ability to specify the PHP version via a .valetphprc
file in the project root to make it easier working with the different versions of PHP that some projects may use.
Just make sure you're using Laravel Valet v2.16.0 or higher on macOS and then add this simple script in your ~/.zshrc
file:
autoload -U add-zsh-hook
load-valetphprc() {
[[ -a .valetphprc ]] || return
local php_version=$(php -r "echo PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;")
local valetphprc_path=".valetphprc"
if [ -f "$valetphprc_path" ]; then
local valetphprc_version="$(cat "${valetphprc_path}")"
if [ "php@$php_version" != "$valetphprc_version" ]; then
valet use
fi
fi
}
add-zsh-hook chpwd load-valetphprc
Now when you cd
into your project directory, if a .valetphprc
file with a valid version of PHP is found (e.g. php@7.4
) the valet use
command will be run automatically giving you one less thing to think about when working across multiple projects.