空我有這樣的路線在我的路線/ web.phpLaravel 5.5請求是在寧靜的控制器
Route::resource('/api/surveys', 'SurveyController');
由於文件說,它創造了API所有需要的路由。這是一個功能,當我去/ API /調查路由被執行:它
public function index()
{
$request = request();
if(!$request->hasHeader('token')) {
return "No auth token found.";
}
$tokenCheck = $this->userService->isTokenValid($request->header('token'));
if($tokenCheck !== true) {
return $tokenCheck;
}
return $this->surveyService->all();
}
什麼,它會檢查,如果令牌頭參數設置,如果沒有,則返回一個錯誤,如果是,它檢查令牌是否有效等。如果一切正常,則應從數據庫返回調查。
public function surveys() {
$request = \Request::create('/api/surveys', 'GET');
$request->headers->set('Accept', 'application/json');
$request->headers->set('token', \Cookie::get('token'));
$response = \Route::dispatch($request);
print_r('<pre>');
print_r($response);
print_r('</pre>');
}
我有一個網站,應該使用我剛創建的API來獲取所有調查記錄。我創建一個新的請求對象,設置標題「令牌」與我從cookie獲得的令牌,然後嘗試分派並獲得響應。但問題是,每次我收到「未找到身份驗證令牌」。錯誤。這意味着$ request-> hasHeader('token')返回false,即使很難,我在我的請求中設置它。如果我在Restful控制器中print_r $ request-> all(),那麼我得到一個空數組。
我試過Postman使用token參數訪問這個API,並且它在郵遞員中工作正常,但是在這裏,似乎Request消失,而它傳播到API控制器。
我在這裏做錯了什麼?
將請求對象作爲索引函數中的參數。 – Amin
我試過了,但它也不起作用。 – cinik11
[見此](https://stackoverflow.com/questions/23505875/laravel-routeresource-vs-routecontroller)。 – Amin