[英]Understanding $_SERVER[REQUEST_URI]
I'm following LARACAST PHP practitioner classes and I had a problem with $_SERVER[REQUEST_URI]. 我正在学习LARACAST PHP实践者类,并且$ _SERVER [REQUEST_URI]遇到问题。
It always returns 404 not found if I write in browser for url: localhost/laracast/about
and I have in router->define just 'laracast' => 'controllers/index.php'
, or some other page after laracast/ except index. 如果我在浏览器中写URL: localhost/laracast/about
并且在router-> define 'laracast' => 'controllers/index.php'
或laracast /除索引后的其他页面,它总是返回404找不到。 I had to change it to localhost/laracast/index.php/about
to make it work, and every other page. 我必须将其更改为localhost/laracast/index.php/about
才能使其正常运行,以及其他所有页面。 I've lost all day figuring out this and I still don't understand why it simply doesn't throw an exception I've declared in class if I omit index.php in my routes rather than 404 error. 我整天都在弄清楚这个问题,但我仍然不明白为什么如果我在路由中省略index.php而不是404错误,为什么它根本不会引发在类中声明的异常。 Does it have something with server or what? 服务器上有东西吗?
The code is next: 代码如下:
routes.php routes.php文件
$router->define([
'laracast/index.php' => 'controllers/index.php',
'laracast/index.php/about' => 'controllers/about.php',
'laracast/index.php/about/culture' => 'controllers/about-culture.php',
'laracast/index.php/contact' => 'controllers/contact.php']);
Router class 路由器类
class Router
{
private $routes = [];
public function define($routes){
$this->routes = $routes;
}
public static function load($file){
$router = new static;
require $file;
return $router;
}
public function direct($uri)
{
if(array_key_exists($uri, $this->routes)){
return $this->routes[$uri];
}
throw new Exception("No route defined for this URI");
}
}
and index.php 和index.php
<?php
$query = require 'core/bootstrap.php';
$uri = trim($_SERVER['REQUEST_URI'], '/');
require Router::load('routes.php')
->direct($uri);
With this url http://localhost/laracast/index.php/about
it works just fine, and all the other pages preceded by index.php. 有了这个URL http://localhost/laracast/index.php/about
它就可以正常工作,而所有其他页面都以index.php开头。 Any explanation and/or suggestions, or this is a valid solution? 任何解释和/或建议,或者这是一个有效的解决方案?
Thanks to @Martin Zeitler instructions and doing some research I've make it work. 感谢@Martin Zeitler的指示,并进行了一些研究,使我的工作正常。 In .htaccess file I've just added this peace of code. 在.htaccess文件中,我刚刚添加了这段代码。
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
and now it recognizes routes. 现在它可以识别路线了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.