本文整理汇总了PHP中Illuminate\Http\Request::expectsJson方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::expectsJson方法的具体用法?PHP Request::expectsJson怎么用?PHP Request::expectsJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Http\Request
的用法示例。
在下文中一共展示了Request::expectsJson方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: convertValidationExceptionToResponse
/**
* Create a response object from the given validation exception.
*
* @param \Illuminate\Validation\ValidationException $e
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
if ($e->response) {
return $e->response;
}
$errors = $e->validator->errors()->getMessages();
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()->withInput($request->input())->withErrors($errors);
}
示例2: unauthenticated
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
示例3: expectsJson
/**
* Determine if the current request probably expects a JSON response.
*
* @return bool
* @static
*/
public static function expectsJson()
{
return \Illuminate\Http\Request::expectsJson();
}
示例4: unauthenticated
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson() || $this->isApiRoute($request)) {
return response()->json(['error' => $exception->getMessage()], 401);
}
return redirect()->guest('login');
}
示例5: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
# Skip API related requests
if ($request->expectsJson()) {
return $next($request);
}
return parent::handle($request, $next);
}
示例6: unauthenticated
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $e
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $e)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
} else {
return view('canvas::auth.login');
}
}
示例7: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return \Closure|\Illuminate\Http\RedirectResponse
* @throws \App\Exceptions\Users\UserAlreadyLoggedInException
*/
public function handle(Request $request, \Closure $next)
{
if (app('auth.driver')->check()) {
if ($request->expectsJson()) {
throw new UserAlreadyLoggedInException();
}
return redirect('/');
}
return $next($request);
}
示例8: handle
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, \Closure $next)
{
if (app('auth.driver')->guest()) {
if ($request->expectsJson()) {
throw new UnauthorizedHttpException('Unauthorized');
} else {
return redirect()->guest(route(app('translator')->getLocale() . '.login'));
}
}
return $next($request);
}
示例9: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
*/
public function render($request, Exception $exception)
{
if ($exception instanceof RejectedInputException) {
$errors[$exception->getAttribute()] = 'There was a problem with this field.';
if ($request->expectsJson()) {
return response()->json($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
}
return redirect()->back()->withInput($request->input())->withErrors($errors);
}
return null;
}
示例10: register
/**
* Store a newly created user.
*
* @param \Illuminate\Http\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \App\Exceptions\Common\ValidationException
*/
public function register(Request $request)
{
$validator = app('validator')->make($request->all(), ['name' => 'sometimes|required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|confirmed|min:' . config('auth.passwords.users.min_length')]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$user = new User();
$request->has('name') && ($user->name = $request->input('name'));
$user->email = $request->input('email');
$user->password = app('hash')->make($request->input('password'));
$user->save() && event(new Registered($user));
app('auth.driver')->login($user);
return $request->expectsJson() ? response()->json($user)->setStatusCode(IlluminateResponse::HTTP_CREATED) : redirect($this->redirectPath());
}
示例11: activate
/**
* Activate an account [Web only].
*
* @param \Illuminate\Http\Request $request
* @param string|null $token
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \App\Exceptions\Common\ValidationException
*/
public function activate(Request $request, $token = null)
{
$data = !is_null($token) ? ['token' => $token] : $request->all();
$validator = app('validator')->make($data, ['token' => 'required|string']);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$activation = UserActivation::whereCode($data['token'])->first();
if (!$activation) {
throw new TokenNotValidException();
}
/** @var \App\Models\User $user */
$user = User::findOrFail($activation->user_id);
$this->complete($user, $data['token']);
return $request->expectsJson() ? response()->json(['message' => 'Activated']) : redirect($this->redirectPath())->with('message', 'Activation successful');
}
示例12: buildFailedValidationResponse
/**
* @param \Illuminate\Http\Request $request
* @param array $errors
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function buildFailedValidationResponse(Request $request, array $errors)
{
if ($request->expectsJson()) {
return new JsonResponse($errors, 422);
}
return redirect()->to($this->getRedirectUrl())->withInput($request->input())->withErrors($errors, $this->errorBag());
}
示例13: logout
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
public function logout(Request $request)
{
$user = app('auth.driver')->user();
app('auth.driver')->logout();
$request->session()->flush();
$request->session()->regenerate();
app('events')->fire(new LoggedOut($user));
if ($request->expectsJson()) {
return response()->json([]);
}
return redirect('/');
}
示例14: resetPassword
/**
* Reset the password through password-reset-token and email provided.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
* @throws \App\Exceptions\Common\ValidationException
*/
public function resetPassword(Request $request)
{
$validator = app('validator')->make($request->all(), ['token' => 'required|string', 'email' => 'required|email|max:255', 'password' => 'required|confirmed|min:' . app('config')->get('auth.passwords.users.min_length')]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$credentials = $request->only('email', 'password', 'password_confirmation', 'token');
$passwordBroker = app('auth.password.broker');
$response = $passwordBroker->reset($credentials, function (User $user, $password) {
$user->password = app('hash')->make($password);
$user->save();
app('auth.driver')->login($user);
});
switch ($response) {
case $passwordBroker::INVALID_USER:
throw new ModelNotFoundException(trans($response));
break;
case $passwordBroker::INVALID_TOKEN:
throw new TokenNotValidException(trans($response));
break;
}
event(new ResetPassword(app('auth.driver')->user()));
if ($request->expectsJson()) {
return response()->json(['message' => trans('passwords.reset')]);
}
return redirect($this->redirectPath())->with('message', trans('passwords.reset'));
}