Php 了解$\u服务器[请求\u URI]
我正在学习LARACAST PHP实践者课程,我在$\u服务器[REQUEST\u URI]上遇到了问题 如果我在浏览器中为url:localhost/laracast/about写入,它总是返回404 not found,并且我在路由器->定义'laracast'=>'controllers/index.php',或者在laracast/index之外的其他页面。我不得不将其更改为localhost/laracast/index.php/about以使其正常工作,并且每隔一页都要更改一次。我花了一整天的时间才弄明白这一点,但我仍然不明白,如果我在路由中省略index.php而不是404错误,为什么它不会抛出我在课堂上声明的异常。它和服务器有什么关系吗 代码如下: routes.phpPhp 了解$\u服务器[请求\u URI],php,Php,我正在学习LARACAST PHP实践者课程,我在$\u服务器[REQUEST\u URI]上遇到了问题 如果我在浏览器中为url:localhost/laracast/about写入,它总是返回404 not found,并且我在路由器->定义'laracast'=>'controllers/index.php',或者在laracast/index之外的其他页面。我不得不将其更改为localhost/laracast/index.php/about以使其正常工作,并且每隔一页都要更改一次。我花
$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']);
路由器类
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");
}
}
和index.php
<?php
$query = require 'core/bootstrap.php';
$uri = trim($_SERVER['REQUEST_URI'], '/');
require Router::load('routes.php')
->direct($uri);
使用此urlhttp://localhost/laracast/index.php/about 它工作正常,所有其他页面前面都有index.php。
是否有任何解释和/或建议,或者这是一个有效的解决方案?感谢@Martin Zeitler的指导,并做了一些研究,我已经让它发挥了作用。在.htaccess文件中,我刚刚添加了这段代码
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
现在它可以识别路由。您需要使用mod_rewrite并正确地发送请求。。。因此localhost/laracast/about将变成例如localhost/about。看见