2015-02-10 60 views
5

我的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文件夾結構中使用的是,任何幫助,將不勝感激。

+1

您可以嘗試製作您自己的助手類,並根據您的喜好重寫'public_path'函數並重命名public fulder – manix 2015-02-10 14:23:19

+0

您能舉個例子嗎?這個解決方案看起來有點棘手,我一直認爲Laravel已經適用於各種類型的託管環境,如果不是這種情況,它會令人失望。 – roastedtoast 2015-02-10 14:27:12

+0

最佳解決方案是選定的答案。請花一分鐘閱讀它及其評論,並儘量不要使用無用的類來重載您的應用程序。 – roastedtoast 2015-11-15 22:35:42

回答

13

什麼對我來說是完美加入public/index.php以下三行工作:

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

這在Laracast回答。

+1

在文件開頭的權利? – Asim 2015-11-11 09:45:18

+1

緊跟在''app $ require_once __DIR __。'/ ../private/yourappfolder/bootstrap/app之後。php';''其中「yourappfolder」是您的Laravel文件夾,其中「private」是您的laravel文件夾所在的文件夾。考慮刪除「..」或添加「../ ..」,具體取決於公用文件夾的位置。 – roastedtoast 2015-11-15 22:39:18

+1

@Rubens Mariuzzo它在L 5.2上不適合我。 – 2016-03-18 08:00:47

2

我認爲這可以以不同的方式進行,這是我的。

創建一個新的幫助程序文件。您可以在Services文件夾中創建它:

# app/Services/helper.php 

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

的好地方導入輔助文件是AppServiceProvider駐留在app/Providers/AppServiceProvider.php。使用boot來做到這一點。

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

public文件夾重命名爲private,最後你可以從任何地方調用自己的功能:

$path = private_path(); 
+0

你的意思是AppServiceProvider而不是AuthServiceProvider? – roastedtoast 2015-02-10 17:24:38

+0

是的,AppServiceProvider – manix 2015-02-10 17:25:28

+0

我已經創建了該函數的helper.php文件,並在引導函數中添加了include,接下來應該怎麼做? 「將公共文件夾重命名爲私人文件夾」是什麼意思? – roastedtoast 2015-02-10 17:27:34

0

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的功能,我希望他們會在未來的更新中改變它。