所以我有這個(gè)功能,它將被用作 1 作為工匠命令或 2 作為用戶可以使用的鏈接。這是功能:public function saveAll($whatLocation) { $userSaved = $this->saveUsersData(); $componentSaved = $this->saveComponentsData(); $issueSaved = $this->saveIssuesData(); $timelogSaved = $this->saveTimelogsData(); if($userSaved === 1 && $componentSaved === 1 && $timelogSaved === 1) { $this->allSaved = 1; } if($whatLocation != "artisanCommand") { return redirect()->back(); } else { return $this->allSaved; } }這個(gè)想法是,如果它來(lái)自我創(chuàng)建的工匠控制臺(tái)命令,我會(huì)傳遞“artisanCommand”,但如果它來(lái)自路由,它會(huì)拉取我的 api 數(shù)據(jù)并重定向到上一頁(yè)。這是問(wèn)題所在:在網(wǎng)絡(luò)路由文件中,我現(xiàn)在不能調(diào)用具有參數(shù)的函數(shù)?Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll'])->name('pull-and-save-api-data');如果函數(shù)上沒(méi)有參數(shù),這可以工作,但現(xiàn)在有參數(shù)就不行了?我試過(guò)了:Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll("notcommand")'])->name('pull-and-save-api-data');Route::get('/pull-and-save-api-data/{notcommand}', [App\Classes\SaveApiData::class, 'saveAll'])->name('pull-and-save-api-data');既不工作?
1 回答

人到中年有點(diǎn)甜
TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
由于您沒(méi)有從路由傳遞給它的值,因此您只需為該函數(shù)創(chuàng)建一個(gè)默認(rèn)參數(shù)怎么樣?
public function saveAll($whatLocation = null)
我不會(huì)直接從這樣的路線調(diào)用你的方法;讓路由轉(zhuǎn)到控制器并讓該控制器與您需要的類/庫(kù)進(jìn)行交互。
如果你真的必須從路由中傳遞一個(gè)值,你可以設(shè)置一個(gè)默認(rèn)值:
Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll'])
->name('pull-and-save-api-data')
->defaults('whatLocation', 'notArtisan');
- 1 回答
- 0 關(guān)注
- 135 瀏覽
添加回答
舉報(bào)
0/150
提交
取消