通过 ajax 进行 Laravel 身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32959474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel auth via ajax
提问by Mohammad
i write Controller for management ajax requests . i write construct method for check user login or not if
block work fine but always return user.setting
view and return in construct not work .
我为管理 ajax 请求编写控制器。我编写了用于检查用户登录或不if
阻止工作正常的构造方法,但始终返回user.setting
视图并在构造中返回不起作用。
controller:
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
class AjaxController extends Controller {
public function __construct() {
if (Auth::check() == FALSE) {
return view('errors.notLogin');
}
}
public function settings() {
return view('user.setting');
}
}
route:
路线:
Route::post('ajax/settings', 'AjaxController@settings');
js:
js:
acc_settings = function (url) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: url,
type: 'POST',
data: 'settings=settings',
success: function (data) {
$("#ajax").html(data);
}
});
};
回答by Gal
You should use middlewareinstead of re-implement the functionality.
您应该使用中间件而不是重新实现功能。
public function __construct()
{
$this->middleware('auth');
}
then inside auth
you could add both cases for ajax & regular http request -
然后在里面auth
你可以为 ajax 和常规 http 请求添加两种情况 -
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return view('errors.notLogin');
}
else
{
return redirect()->guest('auth/login');
}
}
return $next($request);
}
回答by Johnny Magrippis
This looks like a job for Middleware.
这看起来像是中间件的工作。
You shouldn't do anything in the constructor of your AjaxController, instead you should register a middleware for the route you want to protect:
您不应该在 AjaxController 的构造函数中做任何事情,而应该为要保护的路由注册一个中间件:
Route::post('ajax/settings', [
'uses' => 'AjaxController@settings',
'middleware' => 'auth'
]);
Since you'll probably need many Ajax functions, you could instead group them all with the same middleware, and the "ajax" prefix while you are at it:
由于您可能需要许多 Ajax 函数,因此您可以使用相同的中间件和“ajax”前缀将它们全部分组:
Route::group(['prefix' => 'ajax', 'middleware' => 'auth'], function () {
Route::post('settings', 'AjaxController@settings');
//Define more routes here...
});
'auth' is one of the preset middleware shipped with Laravel, you can find it at App\Http\Middleware\Authenticate and modify it as needed, or register your own "Ajax" middleware. In either case, your handle function would look like:
'auth' 是 Laravel 自带的预置中间件之一,你可以在 App\Http\Middleware\Authenticate 找到它并根据需要修改它,或者注册你自己的“Ajax”中间件。在任何一种情况下,您的句柄函数都将如下所示:
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
return view('errors.notLogin');
}
return $next($request);
}
回答by Ján Kyselica
Why don't you use Middleware?
为什么不使用中间件?
Your constructor should be:
你的构造函数应该是:
public function __construct() {
$this->middleware('auth');
}
You can also assign middleware to run on a route in routes.php:
您还可以指定中间件在 routes.php 中的路由上运行:
Route::group(['middleware' => 'auth'], function () {
Route::post('ajax/settings', 'AjaxController@settings');
}