【问题标题】:how to configure xampp server to open php file by default without writing the .php extension如何配置xampp服务器默认打开php文件而不写.php扩展名
【发布时间】:2020-03-24 01:21:57
【问题描述】:

如何配置xampp服务器默认在浏览器中打开php文件而不在window 10中写.php扩展名

【问题讨论】:

  • google 的神奇关键字是 htaccess 和/或 mod_rewrite
  • 这是通过将所有请求重定向到单个文件(例如 index.php)然后 index.php 读取 REQUEST_URI 以计算出它需要执行的控制器和操作来完成的
  • @zanderwar 但 .php 扩展名将保留,因为您的路由器只会重定向到另一个具有 .php 扩展名的 php 脚本;)
  • 不,请看下面我的回答...有点粗糙且未经测试,但它应该可以工作,哈哈
  • 这能回答你的问题吗? .htaccess doesn't work on xampp (windows 7)

标签: php xampp wamp


【解决方案1】:

通过将所有请求重定向到单个文件(例如 index.php)然后 index.php 读取 REQUEST_URI 以计算出它需要执行的控制器和操作来完成

一个(非常粗略但)基本的示例,假设路由的构造类似于“/controller/action/otheraction”:

.htaccess(在项目根目录中)

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ index.php [NC,L]

index.php

<?php
// include the base controller
include 'Controller.php';

// split the request URI into parts
$request = explode('/', $_SERVER['REQUEST_URI']);

$controller = $request[0] ?: 'home'; // home is default
$action = $request[1] ?: 'index'; // index is default action
$otherAction = $request[2] ?: null; // whatever you want

// build the controller we need to instantiate
$controller_name = ucfirst($controller) . 'PageController';

// check it exists
if (file_exists($controllerName)) {
    include_once $controllerName . '.php';
} else {
    // some pretty 404 page
    die('404');
}

// instantiate the controller
$controller = new $controller();

// execute the action
$controller->doAction($action, $otherAction);

控制器.php

<?php

class Controller 
{
    public function doAction($action, $otherAction) 
    {
        if (method_exists($this, $action)) {
            return $this->{$action}($otherAction);
        }

        die('404 - Page Not Found');
    }
}

HomePageController.php

<?php
class HomePageController extends Controller 
{
    public function index() 
    {
        echo 'Hello World';
        exit;
    }
}

然后你可以导航到http://localhost,你应该会看到“Hello World”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多