在控制器之间传递变量并返回 (PHP)
Passing variables between controllers and returning (PHP)
可能是一个非常简单的问题,但我是新手,在尝试找到类似的问题后我仍然不确定:
所以我有一个 AJAX 表单指向:
function postLogin(Request $request){
$this->fatherAuth($request);
return response() -> json(['url' => '/login-ok',], 200);
}
那么我有:
public function fatherAuth($request){
$validator = Validator::make($request->all(), [
'email' => 'required|email',
],[
'email.required' => 'Email needed',
]);
# do some other checks and if there's some auth error:#
return response() -> json(['url' => '/login-bad',], 400);
}
所以我总是收到 200 的响应而不是 400。
我应该将变量传递给 postLogin 吗?我应该将其发送到新功能吗?
顺便说一句,创建 fatherAuth 的原因是因为此代码在多个控制器之间共享。
最佳解决方案/最佳实践是什么?
谢谢
你得到 200 response
因为你没有对 fatherAuth
方法返回的响应做任何事情。
要让它工作,你应该使用类似的东西:
function postLogin(Request $request){
$response = $this->fatherAuth($request);
if ($response instanceof \Illuminate\Http\Response) {
return $response;
}
return response() -> json(['url' => '/login-ok',], 200);
}
但如您所见,这不是最佳方法。
这就是您应该为此使用 middleware 的原因。例如:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// here you do anything you want and assign to $result variable
if (!$result) {
return response() -> json(['url' => '/login-bad',], 400);
}
return $next($request);
}
}
然后你可以将这个中间件应用于添加到 App/Http/Kernel.php
文件中的 $middleware
数组的所有路由:
App\Http\Middleware\CheckAuth::class,
当然,如果需要,您可以仅将此中间件应用于选定的路由。
之后,在您的 postLogin
方法中,只需要:
function postLogin(Request $request){
return response() -> json(['url' => '/login-ok',], 200);
}
可能是一个非常简单的问题,但我是新手,在尝试找到类似的问题后我仍然不确定:
所以我有一个 AJAX 表单指向:
function postLogin(Request $request){
$this->fatherAuth($request);
return response() -> json(['url' => '/login-ok',], 200);
}
那么我有:
public function fatherAuth($request){
$validator = Validator::make($request->all(), [
'email' => 'required|email',
],[
'email.required' => 'Email needed',
]);
# do some other checks and if there's some auth error:#
return response() -> json(['url' => '/login-bad',], 400);
}
所以我总是收到 200 的响应而不是 400。
我应该将变量传递给 postLogin 吗?我应该将其发送到新功能吗?
顺便说一句,创建 fatherAuth 的原因是因为此代码在多个控制器之间共享。
最佳解决方案/最佳实践是什么?
谢谢
你得到 200 response
因为你没有对 fatherAuth
方法返回的响应做任何事情。
要让它工作,你应该使用类似的东西:
function postLogin(Request $request){
$response = $this->fatherAuth($request);
if ($response instanceof \Illuminate\Http\Response) {
return $response;
}
return response() -> json(['url' => '/login-ok',], 200);
}
但如您所见,这不是最佳方法。
这就是您应该为此使用 middleware 的原因。例如:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// here you do anything you want and assign to $result variable
if (!$result) {
return response() -> json(['url' => '/login-bad',], 400);
}
return $next($request);
}
}
然后你可以将这个中间件应用于添加到 App/Http/Kernel.php
文件中的 $middleware
数组的所有路由:
App\Http\Middleware\CheckAuth::class,
当然,如果需要,您可以仅将此中间件应用于选定的路由。
之后,在您的 postLogin
方法中,只需要:
function postLogin(Request $request){
return response() -> json(['url' => '/login-ok',], 200);
}