简体   繁体   English

AltoRouter未正确执行路由

[英]AltoRouter not doing the routing correctly

I'm just developing a little WebApp with PHP and for routing, I use AltoRouter . 我只是用PHP开发一个WebApp,并且使用AltoRouter进行路由。

So far, I was only developing and testing offline, so on my localhost. 到目前为止,我只在离线环境中进行开发和测试,因此在本地主机上也是如此。 Now, to see the behaviour on a server, I uploaded my code, fixed the database connection to fit for the MySQL-Server running on the server (which is Ubuntu 14.04) and tried to get it started. 现在,要查看服务器上的行为,我上传了代码,修复了数据库连接以适合服务器上运行的MySQL-Server(Ubuntu 14.04)并尝试使其启动。

First, what I'm doing for the routing, is a simple .htaccess , looking like that: 首先,我为路由所做的是一个简单的.htaccess ,如下所示:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L] 

And then, in my index.php , the routing is done 然后,在我的index.php ,完成路由

<?php 

include 'vendor/altorouter/altorouter/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('');

$router->map('GET','/','home.php','home');
$router->map('GET','/datenschutz','datenschutz.php','datenschutz');
$router->map('GET','/impressum','impressum.php','impressum');

$router->map('GET','/header','header.php','header');
$router->map('GET','/jquery','vendor/components/jquery/jquery.js','jquery');
$router->map('GET','/bootstrapjs','vendor/twbs/bootstrap/dist/js/bootstrap.js','bootstrapjs');
$router->map('GET','/bootstrapcss','vendor/twbs/bootstrap/dist/css/bootstrap.css','bootstrapcss');
$router->map('GET','/generalStyle','style.css','generalStyle');
$match = $router->match();
if($match) {
  if($match['name'] === 'generalStyle'){
    header("Content-Type: text/css");
    $fileName = $match['target'];
    echo file_get_contents($fileName);
    return;
  }
  require $match['target'];
}
else {
  header("HTTP/1.0 404 Not Found");
  require '404.php';
}

?>

This is perfectly working on my localhost, so there doesn't appear to be a problem in general. 这在我的本地主机上完全可以正常运行,因此总体上似乎没有问题。

However, on my server, when heading forward to the IP, it first seems, that it's working, because when only going to / , the home.php file is loaded correctly. 但是,在我的服务器上,当转至IP时,首先看起来它正在运行,因为仅当转到/home.php文件home.php正确加载。 But, whatever other route I call, like /impressum , it's not working, going back to / , the home.php is working again. 但是,我叫什么其他途径,如/impressum ,它不工作,要回/时, home.php重新工作。

Now, replacing home.php (in the route) with impressum.php is working as well, then I can see the correct file as well. 现在,也可以用impressum.php替换home.php (在路由中),然后我也可以看到正确的文件。 So to summarize, it's seems like it's always only the first route that is working. 综上所述,似乎它始终只是起作用的第一条路线。

Does anybody have an idea, why this happens or what the problem is? 是否有人有想法,为什么会发生或问题是什么?

I tried to call the files directly, and (besides some scripts not loaded etc.) its working, so going to myDomain.com/impressum.php is working fine, so it doesn't seem to be a problem of the file or sth. 我试图直接调用文件,并且(除了一些未加载的脚本等)可以正常工作,因此转到myDomain.com/impressum.php可以正常工作,因此似乎不是文件或其他问题。

I also tried to set the permission of the hole project to 755, to be sure it's not a problem with that, same results though. 我还尝试将Hole项目的权限设置为755,以确保这不是问题,但结果相同。

Any ideas? 有任何想法吗?

EDIT: I've been trying around a lot, but still didn't got it working.... However, I figured, that it's not "the first route" working, but only the / -route, which is working. 编辑:我一直在尝试很多,但仍然没有使它工作。...但是,我认为,这不是“第一条路线”正在工作,而只有/ -route起作用了。 Every other route isn't working, but again, everything is working on localhost with XAMPP running... 其他所有路由均不起作用,但同样,一切都在XAMPP运行的情况下在localhost上运行...

Finally, I solved the problem.. 最后,我解决了这个问题。

It all wasn't about the PHP, AltoRouter, htaccess or whatever... 这全都不是关于PHP,AltoRouter,htaccess或其他任何东西。

The solution was to set AllowOverride to All in the Directory of my vhost -config.... 解决方案是在我的vhost -config的Directory中将AllowOverride设置为All

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

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