简体   繁体   English

PHP 与盖茨比的联系表格

[英]PHP contact form with Gatsby

I'm creating a Gatsby portfolio and I want to include a PHP contact form.我正在创建一个 Gatsby 投资组合,并且我想包含一个 PHP 联系表。 I've seen a lot of examples that use external resources to send the email from the site.我已经看到很多使用外部资源从站点发送 email 的示例。 An example is ' Getform ' which is referenced in the Gatsby docs.一个例子是在 Gatsby 文档中引用的“ Getform ”。 But I'd prefer to not have to rely on an external resource and keep the PHP script within my project.但我宁愿不必依赖外部资源并将 PHP 脚本保留在我的项目中。 My question is related to my approach.我的问题与我的方法有关。

I'm using an axios POST request to send the form data to a PHP file located on the same server as my project.我正在使用 axios POST 请求将表单数据发送到与我的项目位于同一服务器上的 PHP 文件。 These PHP do not live within my project repo.这些 PHP 不在我的项目仓库中。 Is this the best approach or is there another way to keep the PHP sendmail file within my Gatsby project, so that when I run the build and deploy everything can be pushed to the server without error.这是最好的方法还是有另一种方法可以将 PHP 发送邮件文件保留在我的 Gatsby 项目中,这样当我运行构建和部署时,所有内容都可以毫无错误地推送到服务器。 Ideally I'd like the PHP files to live within my project repo also.理想情况下,我希望 PHP 文件也存在于我的项目存储库中。

You can place your php scripts in /static/api您可以将 php 脚本放在 /static/api

After the build and deployment you can call it with https://domain.tld/api/scriptname.php构建和部署后,您可以使用https://domain.tld/api/scriptname.php调用它

Gatsby move the api directory from the static folder to the root. Gatsby 将 api 目录从 static 文件夹移动到根目录。 :) :)

Gatsby generates a set of static files . Gatsby 生成一 组 static 文件 There's no server-side runtime, so you'll need to run your services separately and call them at client-side runtime using axios, for example, or the fetch browser API.没有服务器端运行时,因此您需要单独运行服务并在客户端运行时使用 axios 或获取浏览器 API 调用它们。

If you absolutely want a single repo, you can probably run a PHP server and serve the built Gatsby site on a given route.如果你绝对想要一个 repo,你可以运行 PHP 服务器并在给定的路由上为构建的 Gatsby 站点提供服务。 However this is far from a typical use of Gatsby, it goes against Gatsby's content mesh philosophy , and it will make your release workflow more complicated (and not simpler).然而,这与 Gatsby 的典型用法相去甚远,它违背了Gatsby 的内容网格哲学,并且会使您的发布工作流程更加复杂(而不是更简单)。

I recommend keeping your services and your Gatsby site separate.我建议将您的服务和 Gatsby 网站分开。

PS: to learn more about the "content mesh", here's a great 3-part article about it on Gatsby's blog . PS:要了解有关“内容网格”的更多信息,请参阅Gatsby 博客上的一篇精彩的 3 部分文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM