[英]htaccess for custom url routing using thephplegue route
我是PHP的新手。 我正在使用来自packagist.org的League / route在我的应用程序中进行路由,并且我在根目录中有一个公用文件夹,在其中我有一个index.php文件用于进行路由,如下所示。
require_once '../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$router = new League\Route\RouteCollection;
$router->addRoute('GET', '/acme', function (Request $request, Response $response) {
echo "here";
return "working";
});
$dispatcher = $router->getDispatcher();
$response = $dispatcher->dispatch('GET', '/acme');
$response->send();
.htaccess文件(如果位于根文件夹中,如下所示)
RewriteEngine On
RewriteBase /HMT/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
问题是当我运行http:// localhost / HMT / acme时,什么都没有显示。 我尝试在public / index.php文件的每一行之后添加一个echo语句,并注意到下面的行是行不通的。 我也认为我的.htaccess可能是问题所在??
$dispatcher = $router->getDispatcher();
感谢您的帮助,谢谢。
因此,这里有两个问题。
似乎您已关闭错误报告,否则将看到某种错误。
将以下代码添加到index.php
的顶部。
error_reporting(-1);
ini_set('display_errors', 'On');
您的.htaccess
实际上应该在您的public
目录中,并按照以下格式设置。
RewriteEngine On
RewriteBase /HMT/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(html)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
然后根据您提供的信息,如果您访问http://localhost/HMT/public
那么所有内容都应正常运行。
根据您可能收到的任何错误以及所使用的league/route
的版本,可能还有进一步的步骤来确保将请求URI正确发送到调度程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.