Laravel Rest Api Vercel
Since we'll be using vercel-php which is PHP runtime for Vercel platform, we need two files index.php vercel.json First file index.php has to be in a folder called api, and vercel.json has to be in project root directory like this laravel-vercelproject api index.php vercel.json
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as Simple, fast routing engine. Powerful dependency injection container.
Deploying your Laravel application to Vercel offers a streamlined, serverless solution for hosting your projects. To deploy to Vercel, you need to create three files apiindex.php, vercel.json, and .vercelignore. apiindex.php. Create a new index.php file inside a new api folder in your Laravel project's root directory. Add the following code
Create a vercel.json file Add our apiindex.php entry point Set up our trusted proxies Ensure the vendor directory is not uploaded Setup a new Vecel project. We'll use the Vercel CLI to create a new project, which we plan to deploy later. Install the Vercel CLI npm i-g vercel Login to Vercel vercel login Create and link a new project
vercel.json set deploy config public92.htaccess config for frontend handling API such as header and url backend api92index.php create new index.php in new folder api, for config main laravel load path app92Providers92RouteServiceProviders.php remove prefix api for normally access api in frontend app like your-appapiendpoint
This simple file redirects to the default index.php file provided by Laravel. This is necessary because Vercel only allows functions to be located in the api directory. Set up our trusted proxies Vercel hosts our code behind a load balancer, which forwards requests to port 80. This can confuse Laravel when it generates secure links.
The API supports any programming language or framework that can send HTTP requests. To interact with the API, you can Use the Vercel SDK by first instantiating with your token Use the language of your choice by calling the endpoints directly and providing your token
env will be our Vercel's env file. Step 4 The last step is to create dist folder in the project directory. Now save all the files and push your repository to GitHub. Now login to your Vercel application. On the right side click Add New then select Project option. After that import your laravel application from GitHub.
To go from stock install to Vercel-ready, we only need to add 3 files to this project apiindex.php vercel.json.vercelignore Let's walk through them one by one. apiindex.php. Create a new index.php file in a new api folder in the root directory of your Laravel app and fill it in with the following contents
The easiest way to do this is change vercel-php version in vercel.json file. Like this - quotapiindex.phpquot quotruntimequot quotemail protectedquot Also change the output directory to quotapiquot. This is my way which fixed this problem. Under the quotBuild Commandquot field, enter the command to build your Laravel project. This command is typically composer