【问题标题】:Change the public folder of Laravel 5.1更改 Laravel 5.1 的公用文件夹
【发布时间】:2015-09-27 09:43:19
【问题描述】:

我正在寻找一种在 laravel 5.1 中更改公共文件夹名称的方法,因为我的主机只允许我使用 htdocs 文件夹。

【问题讨论】:

    标签: laravel laravel-5 laravel-5.1


    【解决方案1】:

    我遵循了这些步骤,在这里提到:https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5

    1. bootstrap/app.php,添加

      $app->bind('path.public', function() {
            return base_path('htdocs');
      });
      
    2. 然后,在/server.php 中,将public 的两个出现更改为htdocs(或任何你想使用的)。

    我真诚地希望这在任何情况下都能奏效。

    编辑 2016-10-18 :

    我最近不得不这样做,但这次我的主机允许我删除“htdocs”文件夹(而且我有 ssh 访问权限):

    • 我将 Laravel 安装在“htdocs”文件夹下方的根文件夹中
    • 我删除了“htdocs”文件夹
    • 我创建了一个符号链接来将“htdocs”映射到“public”:ln -s public htdocs

    【讨论】:

    • @michael,我应该把它放在哪里?
    • 嗯...我注意到我存储在 htdocs 上的文件/某些东西不再可用...仔细查看,我会在介绍完后立即发布更新。
    • 好的,php artisan serve 命令调用 vendor/laravel/framework/src/Illuminate/Foundation/Application.php::publicPath(),其中“public”字符串是硬编码的。我更新了它,但这不是很面向未来,除非它只被工匠服务调用。
    【解决方案2】:

    这就是我的做法.. 更新和其他事情到现在为止都运行良好..

    在根目录创建一个新文件夹.. 并移动其中的所有文件夹(例如,将其称为“myfolder”).. 然后将公用文件夹中的文件移动到根目录.. 所以它应该看起来像..

    /myfolder/
    /index.php
    /server.php
    /.htaccess
    /favicon
    

    所以用文本编辑器打开 server.php 并替换为这段代码..

    <?php
    
    /**
     * Laravel - A PHP Framework For Web Artisans
     *
     * @package  Laravel
     * @author   Taylor Otwell <taylorotwell@gmail.com>
     */
    
    $uri = urldecode(
        parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
    );
    
    // This file allows us to emulate Apache's "mod_rewrite" functionality from the
    // built-in PHP web server. This provides a convenient way to test a Laravel
    // application without having installed a "real" web server software here.
    if ($uri !== '/' && file_exists(__DIR__.'/'.$uri)) {
        return false;
    }
    
    require_once __DIR__.'/index.php';
    

    然后在 index.php 上

    <?php
    
    /**
     * Laravel - A PHP Framework For Web Artisans
     *
     * @package  Laravel
     * @author   Taylor Otwell <taylorotwell@gmail.com>
     */
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | our application. We just need to utilize it! We'll simply require it
    | into the script here so that we don't have to worry about manual
    | loading any of our classes later on. It feels nice to relax.
    |
    */
    
    require __DIR__.'/myfolder/bootstrap/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to illuminate PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
    */
    
    $app = require_once __DIR__.'/myfolder/bootstrap/app.php';
    
    /*
    |--------------------------------------------------------------------------
    | Run The Application
    |--------------------------------------------------------------------------
    |
    | Once we have the application, we can handle the incoming request
    | through the kernel, and send the associated response back to
    | the client's browser allowing them to enjoy the creative
    | and wonderful application we have prepared for them.
    |
    */
    
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    
    $response = $kernel->handle(
        $request = Illuminate\Http\Request::capture()
    );
    
    $response->send();
    
    $kernel->terminate($request, $response);
    

    然后转到“/myfolder/bootstrap/autoload.php”..

    <?php
    
    define('LARAVEL_START', microtime(true));
    
    /*
    |--------------------------------------------------------------------------
    | Register The Composer Auto Loader
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader
    | for our application. We just need to utilize it! We'll require it
    | into the script here so that we do not have to worry about the
    | loading of any our classes "manually". Feels great to relax.
    |
    */
    
    require __DIR__.'/../vendor/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Include The Compiled Class File
    |--------------------------------------------------------------------------
    |
    | To dramatically increase your application's performance, you may use a
    | compiled class file which contains all of the classes commonly used
    | by a request. The Artisan "optimize" is used to create this file.
    |
    */
    
    $compiledPath = __DIR__.'/cache/compiled.php';
    
    if (file_exists($compiledPath)) {
        require $compiledPath;
    }
    

    和 app.php 在同一个文件夹中

    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Create The Application
    |--------------------------------------------------------------------------
    |
    | The first thing we will do is create a new Laravel application instance
    | which serves as the "glue" for all the components of Laravel, and is
    | the IoC container for the system binding all of the various parts.
    |
    */
    
    $app = new Illuminate\Foundation\Application(
        realpath(__DIR__.'/../')
    );
    
    /*
    |--------------------------------------------------------------------------
    | Bind Important Interfaces
    |--------------------------------------------------------------------------
    |
    | Next, we need to bind some important interfaces into the container so
    | we will be able to resolve them when needed. The kernels serve the
    | incoming requests to this application from both the web and CLI.
    |
    */
    
    $app->singleton(
        Illuminate\Contracts\Http\Kernel::class,
        App\Http\Kernel::class
    );
    
    $app->singleton(
        Illuminate\Contracts\Console\Kernel::class,
        App\Console\Kernel::class
    );
    
    $app->singleton(
        Illuminate\Contracts\Debug\ExceptionHandler::class,
        App\Exceptions\Handler::class
    );
    
    /*
    |--------------------------------------------------------------------------
    | Return The Application
    |--------------------------------------------------------------------------
    |
    | This script returns the application instance. The instance is given to
    | the calling script so we can separate the building of the instances
    | from the actual running of the application and sending responses.
    |
    */
    
    return $app;
    

    在开发和生产环境中非常适合我..

    【讨论】:

    • 是的,当您在公用文件夹下没有任何内容时,这似乎是一个不错的选择。谢谢你的方法。
    • 您可以更改这些文件的路径以满足您的需要:)
    猜你喜欢
    • 2016-09-19
    • 2021-07-11
    • 2016-02-23
    • 1970-01-01
    • 2016-12-02
    • 2016-07-02
    • 1970-01-01
    • 2017-05-29
    • 2015-02-08
    相关资源
    最近更新 更多