简体   繁体   English

htaccess使用phplegue路由进行自定义url路由

[英]htaccess for custom url routing using thephplegue route

I am newbie to PHP. 我是PHP的新手。 I am using league/route from packagist.org for routing in my application and i have a public folder in my root directory where I have an index.php file for routing as shown below. 我正在使用来自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();

The .htaccess file if at the root folder and is as below .htaccess文件(如果位于根文件夹中,如下所示)

RewriteEngine On

RewriteBase /HMT/public

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^ index.php [QSA,L]

The problem is when i run http://localhost/HMT/acme nothing is displayed. 问题是当我运行http:// localhost / HMT / acme时,什么都没有显示。 I tried adding an echo statement after each line of the public/index.php file and noted that the line below is the one that does not work. 我尝试在public / index.php文件的每一行之后添加一个echo语句,并注意到下面的行是行不通的。 I also think my .htaccess may be the problem?? 我也认为我的.htaccess可能是问题所在??

$dispatcher = $router->getDispatcher();

Appreciating your help, Thanks. 感谢您的帮助,谢谢。

So there are a couple of issues here. 因此,这里有两个问题。

It seems as though you have error reporting turned off or you would be seeing some sort of error. 似乎您已关闭错误报告,否则将看到某种错误。

Add the following code to the top of your index.php . 将以下代码添加到index.php的顶部。

error_reporting(-1);
ini_set('display_errors', 'On');

Your .htaccess should actually be in your public directory and be formatted as below. 您的.htaccess实际上应该在您的public目录中,并按照以下格式设置。

RewriteEngine On
RewriteBase /HMT/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(html)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Then based on the info you have given, if you access http://localhost/HMT/public then all should work correctly. 然后根据您提供的信息,如果您访问http://localhost/HMT/public那么所有内容都应正常运行。

Depending on any errors you may recieve and the version of league/route that you are using there may be one further step to ensure that the request URI is being sent to the dispatcher correctly. 根据您可能收到的任何错误以及所使用的league/route的版本,可能还有进一步的步骤来确保将请求URI正确发送到调度程序。

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

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