本文整理汇总了PHP中Illuminate\Http\Request::wantsJson方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::wantsJson方法的具体用法?PHP Request::wantsJson怎么用?PHP Request::wantsJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Http\Request
的用法示例。
在下文中一共展示了Request::wantsJson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: postChangePassword
public function postChangePassword()
{
$newPassword = $this->request->input('password');
$token = $this->request->input('spToken');
$validator = $this->loginValidator();
if ($validator->fails()) {
if ($this->request->wantsJson()) {
return $this->respondWithError('Validation Failed', 400, ['validatonErrors' => $validator->errors()]);
}
return redirect()->to(config('stormpath.web.changePassword.uri') . '?spToken=' . $token)->withErrors($validator);
}
$application = app('stormpath.application');
try {
$application->resetPassword($token, $newPassword);
// the password has been changed. Time to fire the
// `UserHasResetPassword` event
//
Event::fire(new UserHasResetPassword());
if ($this->request->wantsJson()) {
return $this->respondOk();
}
return redirect()->to(config('stormpath.web.changePassword.nextUri'));
} catch (\Stormpath\Resource\ResourceError $re) {
if ($this->request->wantsJson()) {
return $this->respondWithError($re->getMessage(), $re->getStatus());
}
return redirect()->to(config('stormpath.web.changePassword.errorUri'))->withErrors(['errors' => [$re->getMessage()]]);
}
}
示例2: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
// 404 page when a model is not found
if ($e instanceof ModelNotFoundException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['error' => 404, 'mensaje' => 'Recurso no encontrado'], 404);
}
return response()->view('errors.404', [], 404);
}
if ($this->isHttpException($e)) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['error' => 404, 'mensaje' => 'Recurso no encontrado!'], 404);
}
return $this->renderHttpException($e);
} else {
// Custom error 500 view on production
if (app()->environment() == 'production') {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['error' => ['exception' => class_basename($e) . ' in ' . basename($e->getFile()) . ' line ' . $e->getLine() . ': ' . $e->getMessage()]], 500);
}
return response()->view('errors.500', [], 500);
}
return parent::render($request, $e);
}
}
示例3: postRegister
public function postRegister()
{
$validator = $this->registerValidator();
if ($validator->fails()) {
if ($this->request->wantsJson()) {
return $this->respondWithError('Validation Failed', 400, ['validatonErrors' => $validator->errors()]);
}
return redirect()->to(config('stormpath.web.register.uri'))->withErrors($validator)->withInput();
}
try {
$registerFields = $this->setRegisterFields();
$account = \Stormpath\Resource\Account::instantiate($registerFields);
$application = app('stormpath.application');
$account = $application->createAccount($account);
if ($this->request->wantsJson()) {
return $this->respondWithAccount($account);
}
if (config('stormpath.web.verifyEmail.enabled') == true) {
return redirect()->route('stormpath.login', ['status' => 'unverified']);
}
if (config('stormpath.web.register.autoAuthorize') == false) {
return redirect()->route('stormpath.login', ['status' => 'created']);
}
$login = isset($registerFields['username']) ? $registerFields['username'] : null;
$login = isset($registerFields['email']) ? $registerFields['email'] : $login;
$result = $this->authenticate($login, $registerFields['password']);
return redirect()->to(config('stormpath.web.register.nextUri'))->withCookies([config('stormpath.web.accessTokenCookie.name') => cookie(config('stormpath.web.accessTokenCookie.name'), $result->getAccessTokenString(), $result->getExpiresIn(), config('stormpath.web.accessTokenCookie.path'), config('stormpath.web.accessTokenCookie.domain'), config('stormpath.web.accessTokenCookie.secure'), config('stormpath.web.accessTokenCookie.httpOnly')), config('stormpath.web.refreshTokenCookie.name') => cookie(config('stormpath.web.refreshTokenCookie.name'), $result->getRefreshTokenString(), $result->getExpiresIn(), config('stormpath.web.refreshTokenCookie.path'), config('stormpath.web.refreshTokenCookie.domain'), config('stormpath.web.refreshTokenCookie.secure'), config('stormpath.web.refreshTokenCookie.httpOnly'))]);
} catch (\Stormpath\Resource\ResourceError $re) {
if ($this->request->wantsJson()) {
return $this->respondWithError($re->getMessage(), $re->getStatus());
}
return redirect()->to(config('stormpath.web.register.uri'))->withErrors(['errors' => [$re->getMessage()]])->withInput();
}
}
示例4: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next, $role, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
if (user($guard)->new && config('user.verify_email')) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect('verify');
}
}
if (!user($guard)->active && config('user.verify_email')) {
throw new InvalidAccountException('Account is not active.');
}
$roles = explode('|', $role);
if (!user($guard)->hasRoles($roles)) {
throw new RolesDeniedException($roles);
}
return $next($request);
}
示例5: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
return redirect('/');
}
if ($e instanceof MethodNotAllowedHttpException) {
return redirect('/');
}
if ($e instanceof NotFoundHttpException) {
return response(view('errors.404'), 404);
}
if ($e instanceof InvalidConfirmationCodeException) {
Session::flash('flash_message', $e->getMessage());
return redirect('/');
}
if ($e instanceof UserNotOwnerOfProjectException) {
Session::flash('flash_message', $e->getMessage());
return redirect(LaravelLocalization::getCurrentLocale() . '/' . trans('routes.create-project'));
}
if ($e instanceof ProjectCompletedException) {
Session::flash('flash_message', $e->getMessage());
return redirect(LaravelLocalization::getCurrentLocale() . '/' . trans('routes.create-project'));
}
if ($e instanceof UserRequiresAuthenticationException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['login' => '<i class="fa fa-exclamation-circle fa-lg"></i>' . trans('create-project-form.login')]);
} else {
Session::flash('flash_message', $e->getMessage());
return redirect('/');
}
}
if ($e instanceof UserHasIncompleteProjectException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['incomplete' => '<i class="fa fa-exclamation-circle fa-lg"></i>' . trans('create-project-form.incomplete')]);
}
}
if ($e instanceof UserAlreadyHasSubmittedProjectException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['pendingProject' => '<i class="fa fa-exclamation-circle fa-lg"></i>' . trans('create-project-form.pending-project')]);
}
}
if ($e instanceof UserHasCurrentLiveProjectException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['liveProject' => '<i class="fa fa-exclamation-circle fa-lg"></i>' . trans('create-project-form.live-project')]);
}
}
if ($e instanceof ProjectNameAlreadyTakenException) {
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['duplicateName' => '<i class="fa fa-exclamation-circle fa-lg"></i>' . trans('create-project-form.duplicate-name')]);
}
}
return parent::render($request, $e);
}
示例6: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->ajax() && !$request->wantsJson()) {
$response = $next($request);
$content = $response->getContent();
$pos = strripos($content, '</body>');
if (false !== $pos) {
$components = config('components');
$newMatches = true;
$matchedComponents = [];
while ($newMatches == true) {
$renderedContent = '';
$newEls = 0;
//loop registered components and add templates as needed.
foreach ($components as $id => $component) {
if (str_contains($content, '<' . $id) && !in_array($id, $matchedComponents)) {
$newEls++;
$matchedComponents[] = $id;
$componentView = view($component['view']);
$renderedContent .= $componentView;
$this->systemJs->import()->item($component['js']);
}
}
$content = substr($content, 0, $pos) . $renderedContent . substr($content, $pos);
if ($newEls === 0) {
$newMatches = false;
}
}
$response->setContent($content);
}
return $response;
}
return $next($request);
}
示例7: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->wantsJson()) {
return $next($request);
}
abort(401);
}
示例8: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
/**
* Response Exception as Json
*
*/
if ($request->wantsJson()) {
$error = new \stdclass();
$error->error = true;
if ($e instanceof NotFoundHttpException) {
$error->code = $e->getStatusCode();
} else {
$error->code = $e->getCode();
}
if ($error->code == 0) {
$error->code = 400;
}
if ($e instanceof ValidatorException) {
$error->message = $e->getMessageBag();
} else {
$error->message = $e->getMessage();
if (\App::environment('local')) {
$error->file = $e->getFile();
$error->line = $e->getLine();
}
}
return response()->json($error, $error->code);
}
return parent::render($request, $e);
}
示例9: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($request->wantsJson()) {
// Define the response
$response = ['errors' => 'Sorry, something went wrong.'];
// If the app is in debug mode
if (config('app.debug')) {
// Add the exception class name, message and stack trace to response
$response['exception'] = get_class($e);
// Reflection might be better here
$response['message'] = $e->getMessage();
$response['trace'] = $e->getTrace();
}
// Default response of 400
$status = 400;
// If this exception is an instance of HttpException
if ($this->isHttpException($e)) {
// Grab the HTTP status code from the Exception
$status = $e->getStatusCode();
}
// Return a JSON response with the response array and status code
return response()->json($response, $status);
}
// Default to the parent class' implementation of handler
return parent::render($request, $e);
}
示例10: render
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($request->wantsJson()) {
return response(['success' => false, 'message' => $e->getMessage()], 404);
}
return parent::render($request, $e);
}
示例11: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($request->ajax() || $request->wantsJson()) {
return $next($request);
}
return new Response('', 400);
}
示例12: deleteKey
/**
* delete key registered (Only this seller)
* @param $id int|string id the virtual product
* @param $res Request object to validate the type of request, action
* @return json
*/
public function deleteKey($id, Request $res)
{
if (!$res->wantsJson()) {
return redirect()->back();
}
$VirtualProduct = VirtualProduct::find($id);
if (!count($VirtualProduct->toArray())) {
return json_encode(['message' => trans('globals.error_not_available')]);
}
$product = Product::find($VirtualProduct->product_id);
if (!count($product->toArray())) {
return json_encode(['message' => trans('globals.error_not_available')]);
}
if ($product->user_id != \Auth::id()) {
return json_encode(['message' => trans('globals.not_access')]);
}
$VirtualProductOrder = VirtualProductOrder::where('virtual_product_id', $VirtualProduct->id)->get();
if (count($VirtualProductOrder->toArray()) > 0) {
return json_encode(['message' => trans('product.virtualProductsController_controller.key_been_sold')]);
}
$VirtualProduct->status = 'cancelled';
$VirtualProduct->save();
$stock = count(VirtualProduct::where('product_id', $product->id)->where('status', 'open')->get()->toArray());
$product->stock = $stock;
if ($stock == 0) {
$product->status = 0;
}
$product->save();
return json_encode(['success' => trans('product.controller.saved_successfully')]);
}
示例13: handle
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function handle($request, Closure $next)
{
if (!$request->ajax() && !$request->wantsJson()) {
abort(415);
}
return $next($request);
}
示例14: tokensMatch
/**
* Determine if the session and input CSRF tokens match when the request is not a json.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function tokensMatch($request)
{
if ($request->wantsJson()) {
return true;
}
return parent::tokensMatch($request);
}
示例15: edit
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit(Request $request, $id)
{
if ($request->wantsJson() || $request->ajax()) {
throw new \Exception('Editing is not supported by AJAX');
}
return view('project.edit')->with(['project' => Project::find($id)]);
}