Laravel 控制器返回索引视图而不是 json 响应
Laravel controller returning index view instead of json response
我正在使用 apache2 和 php artisan serve 命令来测试我的项目。
拥有下一个控制器:
<?php
// file: Http\Controllers\AuthCtrl.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User; // <--
use Illuminate\Support\Facades\Auth;
class AuthCtrl extends Controller
{
public function login(Request $request)
{
$rules = [ 'email' => 'required|exists:users',
'password' => 'required' ];
$request->validate($rules);
$data = [ 'email' => $request->get('email'),
'password' => $request->get('password') ];
if( Auth::attempt($data) )
{
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
}
else
{
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
}
}
}
还有一个简单的模拟 index.php,它使用以下 js 进行 ajax 调用:
function ExecLogin()
{
$.ajax({
method: "POST",
url: "http://127.0.0.1:8000/api/login",
data : {email : "fsd@gmail.com", password: "mypass"}
}).done(
function(data, status)
{
if(data.success == true)
{
localStorage.setItem('appname_token', data.token);
// the following part makes sure that all the requests made later with jqXHR will automatically have this header.
$( document ).ajaxSend(function( event, jqxhr, settings ) {
jqxhr.setRequestHeader('Authorization', "Bearer " + data.token);
});
}
else
{
alert(data.msg);
}
}).fail(function(error){
// handle the error
});
}
正如你所看到的,当代码进入这部分时,(当用户认证OK时),它returns一个JSON响应好了:
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
但是当它没有通过验证时,它进入了这部分,但是响应是index.php text/html,而不是我定义的json响应:
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
我在 ajax 调用的数据对象上收到以下字符串:
"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="top-right links">
<a href="http://127.0.0.1:8000/login">Login</a>
<a href="http://127.0.0.1:8000/register">Register</a>
</div>
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="links">
<a href="https://laravel.com/docs">Docs</a>
<a href="https://laracasts.com">Laracasts</a>
<a href="https://laravel-news.com">News</a>
<a href="https://blog.laravel.com">Blog</a>
<a href="https://nova.laravel.com">Nova</a>
<a href="https://forge.laravel.com">Forge</a>
<a href="https://vapor.laravel.com">Vapor</a>
<a href="https://github.com/laravel/laravel">GitHub</a>
</div>
</div>
</div>
</body>
</html>
"
我是不是漏掉了什么重要的东西?
验证中间件如下所示:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson() ) {
return route('login');
}
}
}
找到罪魁祸首。
首先我尝试设置响应 header 因为:
if (! $request->expectsJson() )
所以请求只接受 json 响应。
然后我得到一个:
POST http://127.0.0.1:8000/api/login 422 (Unprocessable Entity)
所以我去查看了控制器中的验证器:
改变了这个:
$rules = [ 'email' => 'required|exists:users',
'password' => 'required' ];
对此:
$rules = [ 'email' => 'required',
'password' => 'required' ];
而且效果非常好。
我认为验证需要一个已经存在的用户 object,但我传递的是一个不存在的用户(凭据错误)。
我想这就是问题所在。
我在 API 中开发 create
动作时遇到了同样的问题。
我认为问题在这里得到了解答:https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors.
切中要点:“那么,如果传入的请求字段未通过给定的验证规则怎么办?如前所述,Laravel 将自动将用户重定向回他们的以前的位置。 此外,所有验证错误和请求输入将自动闪现到会话中。
考虑到我还没有创建任何前端,没有自定义视图,所以 Laravel 将请求重定向到标准的“欢迎”视图。
我正在使用 apache2 和 php artisan serve 命令来测试我的项目。
拥有下一个控制器:
<?php
// file: Http\Controllers\AuthCtrl.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User; // <--
use Illuminate\Support\Facades\Auth;
class AuthCtrl extends Controller
{
public function login(Request $request)
{
$rules = [ 'email' => 'required|exists:users',
'password' => 'required' ];
$request->validate($rules);
$data = [ 'email' => $request->get('email'),
'password' => $request->get('password') ];
if( Auth::attempt($data) )
{
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
}
else
{
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
}
}
}
还有一个简单的模拟 index.php,它使用以下 js 进行 ajax 调用:
function ExecLogin()
{
$.ajax({
method: "POST",
url: "http://127.0.0.1:8000/api/login",
data : {email : "fsd@gmail.com", password: "mypass"}
}).done(
function(data, status)
{
if(data.success == true)
{
localStorage.setItem('appname_token', data.token);
// the following part makes sure that all the requests made later with jqXHR will automatically have this header.
$( document ).ajaxSend(function( event, jqxhr, settings ) {
jqxhr.setRequestHeader('Authorization', "Bearer " + data.token);
});
}
else
{
alert(data.msg);
}
}).fail(function(error){
// handle the error
});
}
正如你所看到的,当代码进入这部分时,(当用户认证OK时),它returns一个JSON响应好了:
$user = Auth::user();
// the $user->createToken('appName')->accessToken generates the JWT token that we can use
return response()->json([
'user' => $user, // <- we're sending the user info for frontend usage
'token' => $user->createToken('teltask')->accessToken, // <- token is generated and sent back to the front end
'success' => true
]);
但是当它没有通过验证时,它进入了这部分,但是响应是index.php text/html,而不是我定义的json响应:
return response()->json([
'success' => false,
'msg' => 'Credenciales erroneas.'
]);
我在 ajax 调用的数据对象上收到以下字符串:
"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="top-right links">
<a href="http://127.0.0.1:8000/login">Login</a>
<a href="http://127.0.0.1:8000/register">Register</a>
</div>
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="links">
<a href="https://laravel.com/docs">Docs</a>
<a href="https://laracasts.com">Laracasts</a>
<a href="https://laravel-news.com">News</a>
<a href="https://blog.laravel.com">Blog</a>
<a href="https://nova.laravel.com">Nova</a>
<a href="https://forge.laravel.com">Forge</a>
<a href="https://vapor.laravel.com">Vapor</a>
<a href="https://github.com/laravel/laravel">GitHub</a>
</div>
</div>
</div>
</body>
</html>
"
我是不是漏掉了什么重要的东西?
验证中间件如下所示:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson() ) {
return route('login');
}
}
}
找到罪魁祸首。 首先我尝试设置响应 header 因为:
if (! $request->expectsJson() )
所以请求只接受 json 响应。 然后我得到一个:
POST http://127.0.0.1:8000/api/login 422 (Unprocessable Entity)
所以我去查看了控制器中的验证器:
改变了这个:
$rules = [ 'email' => 'required|exists:users',
'password' => 'required' ];
对此:
$rules = [ 'email' => 'required',
'password' => 'required' ];
而且效果非常好。 我认为验证需要一个已经存在的用户 object,但我传递的是一个不存在的用户(凭据错误)。 我想这就是问题所在。
我在 API 中开发 create
动作时遇到了同样的问题。
我认为问题在这里得到了解答:https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors.
切中要点:“那么,如果传入的请求字段未通过给定的验证规则怎么办?如前所述,Laravel 将自动将用户重定向回他们的以前的位置。 此外,所有验证错误和请求输入将自动闪现到会话中。
考虑到我还没有创建任何前端,没有自定义视图,所以 Laravel 将请求重定向到标准的“欢迎”视图。