This topic uses a third-party dependency Nette\Utils as an example to describe how to install dependencies for your PHP code, package and deploy code to Function Compute.
Preparations
Create a code directory for tests and specify a name for the directory. In this example,
mycode
is used.Linux and macOS
Run the
mkdir -p /tmp/mycode
command to create the directory.Windows
Create a folder and name it
mycode
.
In the
mycode
directory, create theindex.php
file.The following code describes the content of the file:
<?php require_once __DIR__ . '/vendor/autoload.php'; use Nette\Utils\Arrays; function handler($event, $context) { return Arrays::contains([1, 2, 3], 1); }
Use Composer to install dependencies and deploy the code package
Prerequisites
PHP and Composer are installed on your machine. You have the permissions to execute Composer commands. For more information about Composer, see Composer.
(Optional) A PHP function is created in the Function Compute console. For more information, see Create a function.
Procedure
Create the composer.json file in the
mycode
directory.The following code shows the content of the file:
{ "require": { "nette/utils": "v3.2.5" } }
Run the
composer install
command in themycode
directory to install dependencies.After the execution is complete, the composer.lock file and the vendor directory are generated in this directory. The downloaded dependencies are stored in the vendor directory.
Package all files in the
mycode
directory.Linux and macOS
Go to the
mycode
directory and runzip code.zip -r ./*
.NoteMake sure that you have the read and write permissions on the directory.
Windows
Go to the
mycode
directory, select all files, right-click the files, and then compress the files into a ZIP package.
NoteMake sure that the
index.php
file that you created is in the root directory of the package.In the Function Compute console, find the function that you want to manage. In the upper-right corner of the function details page, click Upload Code and upload the .zip file that you prepared.
You can also upload a ZIP package when you create a function in the Function Compute console. For more information, see Create a function.
On the Code tab of the function details page, click Test Function.
Use Serverless Devs to install dependencies and deploy code
Before you start
Procedure
Edit the
s.yaml
file in themycode
directory.The following sample code provides an example of the file:
edition: 3.0.0 name: fcDeployApp access: "default" vars: # Global variables region: "cn-hangzhou" resources: hello_world: component: fc3 # The component name props: region: ${vars.region} # For information about how to use variables, visit: https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC. functionName: "testphp" description: 'this is a test' runtime: "php7.2" code: ./ handler: index.handler memorySize: 128 timeout: 30
In the
mycode
directory, add thecomposer.json
file.The following sample code provides an example of the file:
{ "require": { "nette/utils": "^3.0" } }
Run
sudo s build --use-docker
to install dependencies.After the execution is complete, the dependencies and code are deployed to the ./vendor directory.
Run
sudo s deploy
to deploy the project.After the execution, you can deploy your function to Function Compute.
More information
You can also use layers of Function Compute to install dependencies. For more information, see Create a custom layer.