指定不同的 public 路径

Specify a different public path

我的 Laravel 应用程序在私有文件夹中运行,我需要告诉 Laravel public 路径不同。 今天我已经将 Laravel 应用程序从 4.2 升级到 5.0,但我找不到我们指定 public 路径 since the paths.php file doesn't exist anymore in Laravel 5.0.

的位置

在 laravel 4.2 中我们有 /bootstrap/paths.php 文件:

/*
|--------------------------------------------------------------------------
| Public Path
|--------------------------------------------------------------------------
|
| The public path contains the assets for your web application, such as
| your JavaScript and CSS files, and also contains the primary entry
| point for web requests into these applications from the outside.
|
*/

'public' => __DIR__.'/../../../public_html',

我还没有使用 Laravel 5.0 文件夹结构,如有任何帮助,我们将不胜感激。

我认为这可以用不同的方式制作,这是我的。

创建一个新的帮助文件。您可以在 Services 文件夹中创建它:

# app/Services/helper.php

if ( ! function_exists('private_path')){
    function private_path($path = ''){
       return app_path() . 'private/'
    }
}

导入帮助程序文件的好地方是位于 app/Providers/AppServiceProvider.phpAppServiceProvider。使用 boot 这样做。

public function boot()
{
    include __dir__ . "/../Services/helper.php";
}

将文件夹从 public 重命名为 private,最后您可以从任何地方调用您自己的函数:

$path = private_path();

根据 this post,要替换原来的 public 路径,我们需要覆盖应用程序路径:

<?php namespace App;

use Illuminate\Foundation\Application;

class MyApp extends Application {

    protected $appPaths = array();

    /**
     * Create a new Illuminate application instance.
     *
     * @param  array|null $appPaths
     * @return \MyApp
     */
    public function __construct($appPaths = null)
    {
        $this->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();

        if (!is_array($appPaths)) {
            abort(500, '_construct requires paths array');
        }

        if (!isset($appPaths['base'])) {
            abort(500, '_construct requires base path');
        }

        $this->appPaths = $appPaths;

        $this->setBasePath($appPaths['base']);
    }

    /**
     * Set the base path for the application.
     *
     * @param  string  $basePath
     * @return $this
     */
    public function setBasePath($basePath)
    {
        $this->basePath = $basePath;

        $this->bindPathsInContainer();

        return $this;
    }

    /**
     * Bind all of the application paths in the container.
     *
     * @return void
     */
    protected function bindPathsInContainer()
    {
        $this->instance('path', $this->path());

        foreach (['base', 'config', 'database', 'lang', 'public', 'storage'] as $path)
        {
            $this->instance('path.'.$path, $this->{$path.'Path'}());
        }
    }

    /**
     * Get the path to the application "app" directory.
     *
     * @return string
     */
    public function path()
    {
        return $this->basePath.'/app';
    }

    /**
     * Get the base path of the Laravel installation.
     *
     * @return string
     */
    public function basePath()
    {
        return $this->basePath;
    }

    /**
     * Get the path to the application configuration files.
     *
     * @return string
     */
    public function configPath()
    {
        if (isset($this->appPaths['config'])) {
            return $this->appPaths['config'];
        }
        return $this->basePath.'/config';
    }

    /**
     * Get the path to the database directory.
     *
     * @return string
     */
    public function databasePath()
    {
        if (isset($this->appPaths['database'])) {
            return $this->appPaths['database'];
        }
        return $this->basePath.'/database';
    }

    /**
     * Get the path to the language files.
     *
     * @return string
     */
    public function langPath()
    {
        if (isset($this->appPaths['lang'])) {
            return $this->appPaths['lang'];
        }
        return $this->basePath.'/resources/lang';
    }

    /**
     * Get the path to the public / web directory.
     *
     * @return string
     */
    public function publicPath()
    {
        if (isset($this->appPaths['public'])) {
            return $this->appPaths['public'];
        }
        return $this->basePath.'/public';
    }

    /**
     * Get the path to the storage directory.
     *
     * @return string
     */
    public function storagePath()
    {
        if (isset($this->appPaths['storage'])) {
            return $this->appPaths['storage'];
        }
        return $this->basePath.'/storage';
    }
}

这对我来说似乎很奇怪,正如 post 中提到的那样,感觉我们在 Laravel 的功能上退步了,我希望他们将来会改变它更新。

对我来说完美无缺的是向 public/index.php 添加以下三行:

$app->bind('path.public', function() {
    return __DIR__;
});

已在 Laracast 回答此问题。