You should edit composer.json within the function's handler folder, and add any required package dependencies, referring to the Composer Documentation for instructions on using composer.json.
Refer to the PHP Template Documentation for instructions on how to use ComposersCOMPOSER_AUTH environment variable to configure access to dependencies in private repositories.
In some cases, you may need to use private composer repositories - using the faas-cli you can pass in
a build argument during build, for example;
The PHP template is based upon the Docker Library PHP image and provides the php-extension.sh script which exposes the ability to customise extensions installed in a function image.
The following modules are preinstalled within the function:
A common use-case for static files is when you want to serve HTML, lookup information from a JSON manifest or render some kind of templates.
With the php8 template, static files and folders can just be added to the handler src directory and will be copied into the function image.
To read a file e.g data.json back at runtime you can do the following:
<?phpnamespaceApp;/** * Class Handler * @package App */classHandler{/** * @param string $data * @return string */publicfunctionhandle(string$data):string{// Check if Http_Path environment variable equals '/static'$httpPath=$_ENV['Http_Path']??'';if($httpPath=='/static'){try{// Get the directory where this file is located and go up one level$dataFilePath=dirname(__DIR__).'/data.json';// Check if the file existsif(!file_exists($dataFilePath)){returnjson_encode(['error'=>'data.json file not found','statusCode'=>404]);}// Read the data.json file$fileContent=file_get_contents($dataFilePath);if($fileContent===false){returnjson_encode(['error'=>'Failed to read data.json file','statusCode'=>500]);}// Return the JSON datareturn$fileContent;}catch(Exception$e){returnjson_encode(['error'=>'An error occurred: '.$e->getMessage(),'statusCode'=>500]);}}else{return$data;}}}