0

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 .

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:

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);
        }
    });
};
0

3 Answers 3

3

This looks like a job for Middleware.

Laravel Docs on 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:

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:

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:

public function handle($request, Closure $next)
{
    if ($this->auth->guest()) {
        return view('errors.notLogin');
    }

    return $next($request);
}
Sign up to request clarification or add additional context in comments.

Comments

3

You should use middleware instead of re-implement the functionality.

public function __construct()
{
    $this->middleware('auth');
}

then inside auth you could add both cases for ajax & regular http request -

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);
}

Comments

0

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:

Route::group(['middleware' => 'auth'], function () {
    Route::post('ajax/settings', 'AjaxController@settings');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.