Laravel 5.4 - 构造中的中间件不能在ajax请求中工作

时间:2017-02-24 20:25:19

标签: php jquery ajax laravel

4并且遇到ajax请求问题。

这是我的JS:

self.opaque = NO;  
self.backgroundColor = [UIColor clearColor];  

web.php:

$.ajax({
    url: 'get-map',
    type: 'POST',
    dataType: 'json'
});
MapController.php中的

__构造:

Route::group(['middleware' => 'auth'], function () {
    Route::post('/create-character', 'MainController@createCharacter');
    Route::get('/main', 'MainController@main');

    Route::group(['middleware' => 'check.character.exist'], function() {
        Route::get('/game', 'GameController@index');
        Route::post('/get-map', 'MapController@getMapForCharacter');
    });
});
在Controller.php中

和__construct:

class MapController extends Controller {

    private $mapFieldId;

    public function __construct()
    {
        parent::__construct();

        $character = Character::find($this->characterId);
        $this->mapFieldId = $character->map_field_id;
    }
}

在HTTP请求中正常工作但是ajax返回:MapController.php中的ErrorException第18行:尝试获取非对象的属性

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $userId;
    protected $characterId;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (Auth::id()) {
                $this->userId = Auth::id();

                $character = Character::where('user_id', '=', $this->userId)->first();
                if ($character) {
                    $this->characterId = $character->id;
                }
            }

            return $next($request);
        });
    }
}

在调查期间,我发现Controller.php中的中间件无法启动 - 解析器只是跳过它(仅用于ajax请求)。 我不明白为什么。 有什么建议吗?

0 个答案:

没有答案