Laravel 5.0 (5.1) 安裝與目錄調整
This short article describes an approach that enables multiple projects to share the same vendor folder. Well, this also works in Laravel 5.1.
建立新專案的指令如下
composer create-project laravel/laravel laravel-5.0-empty "~5.0.0" --prefer-dist
rm vendor/compiled.php
composer update
php artisan fresh (remove this scaffolding)
安裝後,共 19.3 MB,其中 vendor 下,有4,526 個檔案,共 18.4 MB。
記得設定 config/app.php
中的時區,
// 'timezone' => 'UTC',
'timezone' => 'Asia/Taipei',
在使用 Laravel 5.0 開發網頁時,每次要備份,vendor 目錄下,檔案很多,要備份時,花很多時間來掃描。因此希望將 vendor
由應用程式分開,可以減少備份時間。另外,storage 目錄下的東西,也是無需備分的。
更進一步,再調整一下目錄,也有可能多個 Application 共用同一個 vendor。
此外,index.php 原先是放在 public 的目錄下,目錄不好設定。因而,個人習慣是,將 index.php
改為放在 myapp
的根目錄下,然後將程式放在子目錄下,例如 app-src
下。更進一步,可以將 app-src 移到可透過網頁存取的範圍外的目錄,增加安全性。
調整後,希望目錄長成這樣子
[myapp]
|
+-- [app-src]
| |
| +- [app], [bootstrap], ...
|
+-- [storage]
|
+-- index.php, favicon.ico, ...
[c:/Laravel_5.0_vendor] // for Linux -- [/var/www/Laravel_5.0_vendor]
在此,使用 IIS 6 的 web server,網頁的根目錄為 "\Inetpub\wwwroot"
要修改的檔案內容如下
1. 修改 myapp/index.php
define('BASEDIR', __DIR__.'/app-src');
define('VENDORDIR', 'c:/Laravel_5.0_vendor');
require BASEDIR.'/bootstrap/autoload.php';
$app = require_once BASEDIR.'/bootstrap/app.php';
$app->useStoragePath(__DIR__.'/storage');
若是同一個 Server 上,有多個用 Laravel 建立的專案,因為 cookie 的名稱都叫 laravel_session,會出現 "TokenMismatchException in VerifyCsrfToken.php" 的錯誤,必須修改 config/session.php
,將 cookie 改成不同的設定。
// 'cookie' => 'laravel_session',
'cookie' => 'my_app_session',
另外,使用 php artisan key:generate
產生新的 APP_KEY,修改 .env
中的 APP_KEY。
啟始執行一個 project 後,在 wincache 的統計資料中,顯示 Resolve Path Cache 大約有 500 個 Cached entries,幾乎都是 vendor 下的檔案。雖然可以將 vender 目錄移到專案外,多個專案共用,但 Cache 的數目並不會減少。不過,共用 vendor,對於備份來講,還是比較方便。
2. 修改 app-src/bootstrap/autoload.php
require VENDORDIR.'/autoload.php';
3. 修改 Laravel_5.0_vendor/composer 下的 4 個 autoload 檔,包括 autoload_classmap.php
、autoload_files.php
、autoload_namespaces.php
、autoload_psr4.php
。
$vendorDir = VENDORDIR;
$baseDir = BASEDIR;
調整好後,有可能要重新啟動 web server,才能正常運作。
瀏覽的 url:
http://10.161.86.192/la-tst/myapp/index.php
在此稍加說明,我是用 composer 安裝一個空的 Laravel 5.0 專案。啟動一個新的 AP 時,建立一個新的 App 目錄,直接 copy 整個目錄,然後,進行開發。
假如,執行 composer update
來更新 package,要在進行 update 之後,依上述步驟將前述幾個 composer 下的檔修改後,再用更新後的 vendor
目錄去同步所使用的 vendor
。
假如是使用 Linux,可用 sed
來取代,指令如下。這兩行指令,可以加在 composer.json
中,於更新後,自動執行。
sed -i '0,/^$vendorDir = /{s/$vendorDir.*/$vendorDir = VENDORDIR;/}' vendor/composer/autoload_[cfnp]*
sed -i '0,/^$baseDir = /{s/$baseDir.*/$baseDir = BASEDIR;/}' vendor/composer/autoload_[cfnp]*
為了安全性,修改 .htaccess
,禁止使用者存取 app-src
目錄下的任何檔案
# Forbidden to access the files under app-src and return "404 Not Found"
RewriteRule (^|/)app-src(/|$) - [L,R=404]
autoload_static.php 的修改
在使用 PHP 5.6 時,在 vendor/composer
的目錄下,又增加了 autoload_static.php
,其中的路徑也需要修改。
搜尋 「__DIR__ . '/../..' . '/app
」,將其置換為「BASEDIR .'/app
」即可。
composer update
要調整使用的 package,例如使用 recca0120/laravel-tracy
,提供更友善的除錯資訊,,必須使用 composer update
來安裝。我的作法是,保留原來的空的專案,用來產生需要的 vendor 檔,然後,再用新的 vendor 覆蓋原來的 vendor。
Short description
To share the vendor
folder for multiple different Laravel 5.0 projects, the steps are summarized as follows.
- Install a fresh Laravel project following the instructions of document.
- Adjust the directory for convenience in development and maintanance. Create a new directory
app-src
and move all the files and directories exceptpublic
to theapp-src
directory. Then move all the files and directories under thepublic
directory to the root directory of the project. Doing so maybe is not secure, but convenient. - Move the
vendor
directory to somewhere that can be commonly used by multiple projects. - Modify codes to let the adjusted project can work correctly.
- Modify
APP_KEY
and the configuration ofcookie
to be distinguished with other projects.