【问题标题】:How to get the exact project folder in PHP如何在 PHP 中获取确切的项目文件夹
【发布时间】:2018-05-31 03:28:01
【问题描述】:

我确实对此进行了很多研究,但似乎这与其他人不同。所以...我现在拥有的是一个项目文件夹todo-oop,我只想获取该项目的确切位置/网址,所以,我想获取http://localhost/todo-oop/

代码:

<?php
  $project_folder = explode('\\', dirname(__FILE__));
  $root_directory = $_SERVER['SERVER_NAME'] . '/' . end($project_folder);
?>

输出:

localhost/partials/css/todo.css

如果我检查查看源并单击 url,它会返回错误的 url:http://localhost/todo-oop/localhost/partials/css/todo.css

我想实现laravel 使用asset() 辅助函数处理他们的css/js/img 文件的方式。

【问题讨论】:

  • $_SERVER['DOCUMENT_ROOT']
  • @DevinGray 嗨。它会返回C:/wamp/www,这不是我想要的。
  • $_SERVER['PHP_SELF'] 将返回确切的路径,但它不是超级安全。您可以将C:\wamp\www 替换为您的服务器名称
  • 你想得到http://localhost/todo-oop/的具体依据是什么?
  • @smith 抱歉,我不明白你到底想问什么。

标签: php laravel directory wamp


【解决方案1】:

PHP 的一大优点是您可以阅读源代码。想知道asset()函数是怎么工作的,看代码就行了!

// Illuminate/Foundation/helpers.php
function asset($path, $secure = null)
{
    return app('url')->asset($path, $secure);
}

所以你给asset 函数一个你想要包含的路径。 asset 函数依次将它交给容器实例来解析(app()),然后 Laravel 用一大堆魔法启动。简而言之,它只是试图将正确的 URL 解析为您提供的路径。

Laravel 的缺点是使用了很多魔法代码来防止样板。这通常很有用,但很难看到内部发生了什么。框架通常在样板文件和魔术之间进行权衡,Laravel 倾向于尽可能放弃样板文件。

现在给出答案

Laravel 非常聪明的做法是在有人连接到您的应用程序时设置属性/全局/变量。

// bootstrap/app.php
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

// Illuminate/Foundation/Application
public function __construct($basePath = null) 
{
    if ($basePath) {
        $this->setBasePath($basePath);
    }
}

现在,您可以通过调用 base_path() 帮助程序,从应用程序中的任何位置访问此路径(即项目根目录)。这实际上返回了我们刚刚设置的确切路径:

function base_path($path = '')
{
    return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path);
}

希望这个答案对你有所帮助!

【讨论】:

  • 嗨洛克。抱歉,我对您的回答感到困惑。
【解决方案2】:

按照建议,一个简单的解决方案是创建一个配置文件(json、xml 或 php — 我将使用最后一个),其中包含在您的域根目录中找到的重要信息,在本例域名:

/config.php

<?php
define('DS',DIRECTORY_SEPARATOR);
define('ROOT_DIR',__DIR__);
define('INCLUDES',ROOT_DIR.DS.'includes');
define('FUNCTIONS',INCLUDES.DS.'functions');
# Define base domain
define('SITE_URL','//localhost');

这只会结合项目路径(如果已设置)。

/includes/functions/asset.php

function asset($path=false)
{
    # Check if a project folder is set
    $proj     = (defined('PROJECT_NAME'))?  PROJECT_NAME : '';
    # See if the current mode uses SSL
    $protocol = (isset($_SERVER['HTTPS']))? 's' : '';
    # Create a path (if no project, it will leave "//" so you need to replace that)
    $final    = str_replace('//','/','/'.trim($proj,'/').'/'.ltrim($path,'/'));
    # Send back full url
    return "http{$protocol}:".SITE_URL.$final;
}

这基本上就是你会做的。

/index.php

<?php
# Include config
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Include the asset function
include_once(FUNCTIONS.DS.'asset.php');
# Define the current project. If you are always accessing this,
# then you may want to have it in the config, but if you are doing multiple projects
# you can leave the define in the root of each project file
define('PROJECT_NAME','todo-oop');
# Use asset to write the asset path
echo asset('/partials/css/todo.css');

应该这样写:

http://localhost/todo-oop/partials/css/todo.css

如果您没有定义项目:

http://localhost/partials/css/todo.css

如果您只有一个使用单独配置访问的项目,您还可以更改 SITE_URL 以包含项目目录:

define('SITE_URL','//localhost/todo-oop');

正如@Loek 所展示的,您可以使用框架来创建此类路径等,或者您可以创建类(如果您有足够的信心这样做) 基本上可以完成我的工作演示。使用类/框架的好处是它们将具有更大的灵活性和动态交互,但是对于一个非常直接的过程,您可以使用上述方法来完成这些类型的路径。

【讨论】:

    【解决方案3】:

    试试这个

    <?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'].'/'; ?>
    

    根据answer,希望这会奏效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      相关资源
      最近更新 更多