【问题标题】:Specify a different public path指定不同的公共路径
【发布时间】:2016-06-30 21:16:51
【问题描述】:

我的 Laravel 应用程序在私有文件夹中运行,我需要告诉 Laravel 公共路径不同。 今天我将一个 Laravel 应用从 4.2 升级到 5.0,我找不到我们在哪里指定公共路径 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 文件夹结构,任何帮助将不胜感激。

【问题讨论】:

  • 您可以尝试制作自己的 Helper 类,并根据您的喜好覆盖 public_path 函数并重命名公共 fulder
  • 你能举个例子吗?这个解决方案看起来有点棘手,我一直认为 Laravel 已经为每种类型的托管环境而构建,如果不再是这种情况,那将是令人失望的。
  • 最佳解决方案是选定的答案。请花一点时间阅读它及其 cmets,并尽量不要让无用的类使您的应用超载。
  • 您能说得更具体些吗?它应该适用于每个不同的文件夹结构,因为 $app 是在绝对路径中定义的。

标签: laravel-5


【解决方案1】:

完美地为我工作的是在public/index.php 中添加以下三行:

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

Laracast 已回答。

【讨论】:

  • 紧跟在$app = require_once __DIR__.'/../private/yourappfolder/bootstrap/app.php'; 之后,其中“yourappfolder”是您的 Laravel 文件夹,“private”是您的 laravel 文件夹所在的文件夹。考虑删除“..”或添加“../..”,具体取决于您的公用文件夹所在的位置。
【解决方案2】:

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

创建一个新的帮助文件。您可以在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();

【讨论】:

  • 您的意思是 AppServiceProvider 而不是 AuthServiceProvider?
  • 我已经用函数创建了helper.php文件,并在boot函数中添加了include,接下来我该怎么做? “将文件夹从公共重命名为私有”是什么意思?
  • 我认为您想使用“私人”文件夹而不是“公共”文件夹。但是,只需输入您需要的名称或其他名称即可
  • 对我来说就像一个魅力。注意:__dir__ 应该是 __DIR__。否则按书面规定。
【解决方案3】:

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

<?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';
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2023-03-09
    • 2018-12-05
    • 1970-01-01
    • 2021-12-31
    • 2020-05-22
    • 2022-01-21
    相关资源
    最近更新 更多