やりたいこと
LaravelでLaravel Excelを使いたい。
環境
さくらのレンタルサーバ スタンダード
PHP 5.4.29
Laravel 4.2.8
1 2 3 4 5 6 7 |
$ php --version PHP 5.4.29 (cli) (built: Jun 11 2014 18:20:32) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies $ php artisan --version Laravel Framework version 4.2.8 |
前提
Composerがインストールされていること。
やり方
Laravelのインストールディレクトリを「/LARAVEL-ROOT/」とした場合。
1. 「conposer.json」を変更する。
2. 「composer update」を実行する。
3. 「app.php」を変更する。
4. LaravelExcel用の設定ファイルを作成する。
5. 確認する。
1. 「conposer.json」を変更する。
「/LARAVEL-ROOT/conposer.json」に「”maatwebsite/excel”: “1.*”」を追加する。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "type": "project", "require": { "laravel/framework": "4.2.*", "maatwebsite/excel": "1.*" }, "autoload": { ... |
2. 「composer update」を実行する。
PHPExcelとLaravelExcelがインストールされる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ composer update Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version. Loading composer repositories with package information Updating dependencies (including require-dev) - Installing phpoffice/phpexcel (1.8.0) Downloading: 100% - Installing maatwebsite/excel (v1.1.5) Downloading: 100% Writing lock file Generating autoload files Generating optimized class loader Compiling common classes Compiling views |
3. 「app.php」を変更する。
「/LARAVEL-ROOT/app/config/app.php」の
「Autoloaded Service Providers」に「‘Maatwebsite\Excel\ExcelServiceProvider’」を追加する。
「Class Aliases」に「‘Excel’ => ‘Maatwebsite\Excel\Facades\Excel’」を追加する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
/* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. | */ 'providers' => array( 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', 'Illuminate\Cache\CacheServiceProvider', 'Illuminate\Session\CommandsServiceProvider', 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', 'Illuminate\Routing\ControllerServiceProvider', 'Illuminate\Cookie\CookieServiceProvider', 'Illuminate\Database\DatabaseServiceProvider', 'Illuminate\Encryption\EncryptionServiceProvider', 'Illuminate\Filesystem\FilesystemServiceProvider', 'Illuminate\Hashing\HashServiceProvider', 'Illuminate\Html\HtmlServiceProvider', 'Illuminate\Log\LogServiceProvider', 'Illuminate\Mail\MailServiceProvider', 'Illuminate\Database\MigrationServiceProvider', 'Illuminate\Pagination\PaginationServiceProvider', 'Illuminate\Queue\QueueServiceProvider', 'Illuminate\Redis\RedisServiceProvider', 'Illuminate\Remote\RemoteServiceProvider', 'Illuminate\Auth\Reminders\ReminderServiceProvider', 'Illuminate\Database\SeedServiceProvider', 'Illuminate\Session\SessionServiceProvider', 'Illuminate\Translation\TranslationServiceProvider', 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', 'Maatwebsite\Excel\ExcelServiceProvider', ), /* |-------------------------------------------------------------------------- | Service Provider Manifest |-------------------------------------------------------------------------- | | The service provider manifest is used by Laravel to lazy load service | providers which are not needed for each request, as well to keep a | list of all of the services. Here, you may set its storage spot. | */ 'manifest' => storage_path().'/meta', /* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => array( 'App' => 'Illuminate\Support\Facades\App', 'Artisan' => 'Illuminate\Support\Facades\Artisan', 'Auth' => 'Illuminate\Support\Facades\Auth', 'Blade' => 'Illuminate\Support\Facades\Blade', 'Cache' => 'Illuminate\Support\Facades\Cache', 'ClassLoader' => 'Illuminate\Support\ClassLoader', 'Config' => 'Illuminate\Support\Facades\Config', 'Controller' => 'Illuminate\Routing\Controller', 'Cookie' => 'Illuminate\Support\Facades\Cookie', 'Crypt' => 'Illuminate\Support\Facades\Crypt', 'DB' => 'Illuminate\Support\Facades\DB', 'Eloquent' => 'Illuminate\Database\Eloquent\Model', 'Event' => 'Illuminate\Support\Facades\Event', 'File' => 'Illuminate\Support\Facades\File', 'Form' => 'Illuminate\Support\Facades\Form', 'Hash' => 'Illuminate\Support\Facades\Hash', 'HTML' => 'Illuminate\Support\Facades\HTML', 'Input' => 'Illuminate\Support\Facades\Input', 'Lang' => 'Illuminate\Support\Facades\Lang', 'Log' => 'Illuminate\Support\Facades\Log', 'Mail' => 'Illuminate\Support\Facades\Mail', 'Paginator' => 'Illuminate\Support\Facades\Paginator', 'Password' => 'Illuminate\Support\Facades\Password', 'Queue' => 'Illuminate\Support\Facades\Queue', 'Redirect' => 'Illuminate\Support\Facades\Redirect', 'Redis' => 'Illuminate\Support\Facades\Redis', 'Request' => 'Illuminate\Support\Facades\Request', 'Response' => 'Illuminate\Support\Facades\Response', 'Route' => 'Illuminate\Support\Facades\Route', 'Schema' => 'Illuminate\Support\Facades\Schema', 'Seeder' => 'Illuminate\Database\Seeder', 'Session' => 'Illuminate\Support\Facades\Session', 'SoftDeletingTrait' => 'Illuminate\Database\Eloquent\SoftDeletingTrait', 'SSH' => 'Illuminate\Support\Facades\SSH', 'Str' => 'Illuminate\Support\Str', 'URL' => 'Illuminate\Support\Facades\URL', 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', 'Excel' => 'Maatwebsite\Excel\Facades\Excel', ), |
4. LaravelExcel用の設定ファイルを作成する。
artisan を使って、LaravelExcel用の設定ファイルを「/LARAVEL-ROOT/app/config/packages/」以下に作成する。
xlsファイルをインポートする際の読み込み方法(force_sheets_collection)とかを変更する用。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ php artisan config:publish maatwebsite/excel $ cd/LARAVEL-ROOT/app/config/packages/ $ tree /LARAVEL-ROOT/app/config/packages/ /LARAVEL-ROOT/app/config/packages/ └── maatwebsite └── excel ├── cache.php ├── config.php ├── csv.php ├── export.php ├── import.php └── views.php |
「import.php」の「heading」「force_sheets_collection」を変更しておく。(任意)
1 2 3 4 5 6 7 8 |
//シートの1行目を(LaravelExcel的に)ヘッダ行として扱わないようにする。 'heading' => 'false', /* * シートが1つしか無い場合でも、必ずsheetコレクションを返すようにする。 * デフォルト(false)だと、1シートしか無い場合にrowコレクションが返ってきてしまう。 */ 'force_sheets_collection' => true, |
5. 確認する。
「/LARAVEL-ROOT/app/route.php」に以下のコードを追加して、LaravelExcelが取れてればOK。
1 2 3 4 5 6 |
Route::get('/laravel-excel', function() { $excel = App::make('excel'); dd($excel); return true; }); |
備考
上記3の手順でapp.phpを変更していたところ、「Class ‘Maatwebsite\Excel\ExcelServiceProvider’ not found.」のエラーが出た。
「composer update」やら「php artisan dump-autoload」をテキトーに叩いていたら直ってしまった。
1 2 3 |
$ composer update $ composer update --no-scripts $ php artisan dump-autoload |