如何从 magento2 中的文档根目录 运行 自定义 php 脚本
How to run custom php script from doc root in magento2
我想将自定义 php 脚本添加到 magento2 根文件夹,运行 来自 browser.I 试图将它添加到 magento2 根文件夹,但它重定向到 404 页面。
我也试过将它添加到 pub 文件夹中,但没有成功。
还清除了缓存和清空生成文件夹。
我正在使用 nginx 服务器
如果您使用的是 magento 附带的 nginx 配置,则需要在 pub
文件夹中放置一个文件,以允许从浏览器访问它,因为 pub 是虚拟主机的文档根目录。 Magento 根目录是上一级。其次,nginx 的所有默认配置仅允许访问 index.php, get.php, static.php, report.php, 404.php
和 503.php
文件。 php 不处理其他任何内容。你可以在nginx.conf.sample
中看到这个符合location ~ (index|get|static|report|404|503)\.php$ {
。如果您不使用它,请检查您的配置是否有类似的规则。要允许从浏览器访问另一个文件,只需在 503 之后添加另一个名称或使用 location ~* \.php$ {
更改整个括号
例如,您可以通过此步骤在自定义脚本中获取产品名称
第 1 步:在 magento 2
的根目录创建 index.php
magento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
步骤 2:创建 customScript.php
magento2/test/customScript.php
<?php
class customScript
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 12;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
现在您可以通过
运行这个自定义脚本
http://10.16.16.196/magento2/test/
如@Ranjit 所述,/pub
文件夹 必须是 您的 Magento 根文件夹。
在 Magento 上 运行 独立 php 脚本的正确方法是:
在 nginx 上:
找到 location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
并在那里添加您的文件。
即:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|myphp)\.php$ {
然后您可以访问yourstore.com/myphp.php.
在 Apache 上:
只需将文件添加到/pub
文件夹下即可。即:/pub/myphp.php
.
如果文件或文件夹不存在,Apache 重写规则将重定向到 index.php
。
在我的 Apache (cPanel) 案例中,问题是 .php 文件的文件权限不应由组或其他人直接写入,否则 Magento 的 404 将打开。
因此,在我的情况下,要直接提供文件,我必须将文件权限设置为 -rw-r--r--
(在 Linux 上)。
这是使用 suPHP 的服务器的主要问题。
把它放在这里以防有人遇到同样的情况...
我想将自定义 php 脚本添加到 magento2 根文件夹,运行 来自 browser.I 试图将它添加到 magento2 根文件夹,但它重定向到 404 页面。
我也试过将它添加到 pub 文件夹中,但没有成功。
还清除了缓存和清空生成文件夹。
我正在使用 nginx 服务器
如果您使用的是 magento 附带的 nginx 配置,则需要在 pub
文件夹中放置一个文件,以允许从浏览器访问它,因为 pub 是虚拟主机的文档根目录。 Magento 根目录是上一级。其次,nginx 的所有默认配置仅允许访问 index.php, get.php, static.php, report.php, 404.php
和 503.php
文件。 php 不处理其他任何内容。你可以在nginx.conf.sample
中看到这个符合location ~ (index|get|static|report|404|503)\.php$ {
。如果您不使用它,请检查您的配置是否有类似的规则。要允许从浏览器访问另一个文件,只需在 503 之后添加另一个名称或使用 location ~* \.php$ {
例如,您可以通过此步骤在自定义脚本中获取产品名称
第 1 步:在 magento 2
的根目录创建 index.phpmagento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
步骤 2:创建 customScript.php
magento2/test/customScript.php
<?php
class customScript
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 12;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
现在您可以通过
运行这个自定义脚本http://10.16.16.196/magento2/test/
如@Ranjit 所述,/pub
文件夹 必须是 您的 Magento 根文件夹。
在 Magento 上 运行 独立 php 脚本的正确方法是:
在 nginx 上:
找到 location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
并在那里添加您的文件。
即:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|myphp)\.php$ {
然后您可以访问yourstore.com/myphp.php.
在 Apache 上:
只需将文件添加到/pub
文件夹下即可。即:/pub/myphp.php
.
如果文件或文件夹不存在,Apache 重写规则将重定向到 index.php
。
在我的 Apache (cPanel) 案例中,问题是 .php 文件的文件权限不应由组或其他人直接写入,否则 Magento 的 404 将打开。
因此,在我的情况下,要直接提供文件,我必须将文件权限设置为 -rw-r--r--
(在 Linux 上)。
这是使用 suPHP 的服务器的主要问题。
把它放在这里以防有人遇到同样的情况...