2015-10-11 108 views
0

我是PHP的新手。我正在使用packagist.org的联盟/路由在我的应用程序中进行路由,并且在我的根目录中有一个公用文件夹,其中有一个index.php文件用于路由,如下所示。htaccess自定义url路由使用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(); 

感谢您的帮助,谢谢。

+0

您应该会收到一条错误消息。尝试在索引文件的开头添加以下内容:ini_set('display_errors',1);'用于调试目的。 – nebulousGirl

回答

3

所以这里有几个问题。

看起来您好像关闭了错误报告,或者您会看到某种错误。

将以下代码添加到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正确发送到调度程序。

+0

谢谢。如果我访问“/ acme”,出现错误“内部服务器错误”,当我访问“/”时出现错误“Catchable致命错误:传递给League \ Route \ RouteCollection :: getDispatcher()的参数1必须是Psr \ Http \ Message \ ServerRequestInterface实例,没有给出。“ – Erick

+0

没错,这是因为你的index.php文件设置为使用版本1的联盟/路线,但实际上你已经安装了未完成的版本2 – philipobenito