Laravel 文字列変数によってオブジェクトのインスタンスを生成する方法
解決したいこと
Laravel/PHPのアプリケーションを開発している66歳の初心者です。
所定の文字列変数によって、オブジェクトのインスタンスを生成しようとして、過去の記事を参考にしてコードを記述しましたが、エラーになりうまく行きません。現在はやむを得ず、Switch-Caseで分岐させていますが、コード効率が良くありません。
ただしい記述方法を教えて頂けると大変助かります。
発生している問題・エラー
Error
Class "KlassExport" not found
http://localhost/backoffices/backup
該当するソースコード コメントアウトしてあるのはSwitch-Caseで対処している動作するコードです。
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\KlassExport;
....中略....
class BackupController extends Controller
{
....中略....
public function backup (Request $request)
// Excel file nameは DBのYYYYMMDD-HHMM_データベース略称.xlsx
// リカバー時にはデータベース略称を自動判別して読み込む
{
$user = Auth::user();
$data = $request->all();
unset($data['_token']);
$today = date ("Ymd-His");
$name = Str::singular($data['select']);
$callname = ucfirst($name) . "Export";
$new = new $callname();
return Excel::download ($new, $today . $name . ".xlsx");
// switch ($data['select'])
// {
// case 'klasses' : return Excel::download (new KlassExport, $today . "_klass.xlsx");
// case 'allshares' : return Excel::download (new AllshareExport, $today . "_allshare.xlsx");
// case 'allsharebases':return Excel::download (new AllsharebaseExport,$today . "_allsharebase.xlsx");
// case 'owners' : return Excel::download (new OwnerExport, $today . "_owner.xlsx");
// case 'transactions': return Excel::download (new TransactionExport, $today . "_transaction.xlsx");
// case 'contacts' : return Excel::download (new ContactExport, $today . "_contact.xlsx");
// case 'oppools' : return Excel::download (new OppoolExport, $today . "_oppool.xlsx");
// case 'opgrants' : return Excel::download (new OpgrantExport, $today . "_opgrant.xlsx");
// case 'alloptions' : return Excel::download (new AlloptionExport, $today . "_alloption.xlsx");
// case 'opowners' : return Excel::download (new OpownerExport, $today . "_opowner.xlsx");
// case 'shmeetings' : return Excel::download (new ShmeetingExport, $today . "_shmeeting.xlsx");
// case 'corpprofiles': return Excel::download (new CorpprofileExport, $today . "_corpprofile.xlsx");
// case 'voterecords' : return Excel::download (new VoteRecordExport, $today . "_voterecord.xlsx");
// case 'users' : return Excel::download (new UserExport, $today . "_user.xlsx");
// case 'whole' : return Excel::download (new WholeExport, $today . "_whole.xlsx");
// default: return;
// }
}
自分で試したこと
下記の情報を参考にして、行く通りかのパターンを試したのですがいずれも同じエラーになります。
Switch-Caseで動作しているのですが、動作するコードを作ったら「リファクタリング」すべきとのベストプラクティスの記事を拝見して、色々書き直しを検討している中で教えて頂きたく、どうぞよろしくお願いいたします。
https://qiita.com/gorogoroyasu/items/d8a6e032eb4debb147ca
https://www.php.net/manual/ja/language.oop5.basic.php#language.oop5.basic.new