drus39136 2019-02-05 00:33
浏览 213

了解$ _SERVER [REQUEST_URI]

I'm following LARACAST PHP practitioner classes and I had a problem with $_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. I had to change it to localhost/laracast/index.php/about to make it work, and every other page. 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. Does it have something with server or what?

The code is next:

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

<?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. Any explanation and/or suggestions, or this is a valid solution?

  • 写回答

1条回答 默认 最新

  • duansha3771 2019-02-05 11:39
    关注

    Thanks to @Martin Zeitler instructions and doing some research I've make it work. In .htaccess file I've just added this peace of code.

    <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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100