我开发了一个API,并且在此页面上遇到了问题,我将其添加到了自己的路线中,并且总是出现 404错误,我不知道为什么
这是我的控制器:
class InsertPPictureController extends Controller
{
public function profilepicture (Request $request)
{
$input = $request->all();
$validator = Validator::make($input, [
'id_user'=> 'required',
'picture'=> 'image|nullable|max:1999'
] );
$user = User::findOrFail($request->id);
$user_id = $request->id ;
if($request->hasFile('picture')){
// Get filename with the extension
$filenameWithExt = $request->file('picture')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('picture')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $user_id.'.'.$extension;
// Upload Image
$path = $request->file('picture')->storeAs('public/profilepic', $fileNameToStore);
$user->pic_path = $fileNameToStore ;
$user->update();
} else {
$fileNameToStore = 'noimage';
}
return response()->json(' Success : User updated with success ',200);
}
}
这是我的api.php
Route::group(['middleware' => ['jwt.verify']], function() {
Route::get('logout', 'AuthController@logout');
Route::post('postcreditscards', 'CreditsCardsController@stockcards');
Route::get('getcreditscards', 'CreditsCardsController@index');
Route::get('getmybalance', 'MyBalanceController@index');
Route::get('getuserdata', 'AuthController@getuser');
Route::post('sendMoneyTransaction', 'MyBalanceController@updatebalance');
Route::post('isvalidnumber', 'AuthController@validnumber');
Route::post('updateuser', 'AuthController@updateuser');
Route::post('insertprofilepicture','InsertPPictureController@profilepicture');
});
所有页面正常工作,仅此页面不工作
Route::post('insertprofilepicture','InsertPPictureController@profilepicture');
答案 0 :(得分:4)
由于控制器代码中的以下行,您收到404错误:
final PopupWindow popupWindow = new PopupWindow();
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert_reply_chat,null);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(180);
popupWindow.setBackgroundDrawable(null);
popupWindow.setClippingEnabled(false);
popupWindow.setTouchable(true);
popupWindow.setContentView(view);
return popupWindow;
您提供的$user = User::findOrFail($request->id);
在用户表中不存在,并且由于您没有捕获到异常,因此Laravel返回404响应,实际上是id
参考here
转到:以上链接的“未找到例外”部分。这是doc中的几行内容:
ModelNotFoundException
答案 1 :(得分:0)
echo '<pre>'.print_r($user,1);die();
只需在 $ user =用户:: findOrFail($ request-> id); 线。我建议您检查数据数组传递给此控制器。如果通过,则可以逐行检查。