Laravel身份验证无法使用多个控制器

时间:2016-02-18 21:41:52

标签: php laravel authentication middleware controllers

嘿伙计们我正在Laravel工作。我面临着有关身份验证的问题 它每当我尝试验证用户并将其重定向到特定控制器的视图时它工作正常,但是当我尝试打开另一个视图时,用户身份验证不起作用(用户注销)。基本上问题是我有两个控制器,所以如果我从身份验证重定向到其中任何一个,另一个没有用户。
我希望我能清楚地解释我的问题

路线列表:结果 Route List

我正在使用主页面路线...其他人现在只是虚拟

这是我的身份验证类

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
/**
 * The Guard implementation.
 *
 * @var Guard
 */
protected $auth;



public function __construct(Guard $auth)
{
    $this->auth = $auth;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/');
        }
    }

    return $next($request);
}
}

这是索引控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class IndexPageController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */

public function __construct()
{

    $this->middleware('auth');

}




public function index()
{

   $user = Auth::user()->firstOrFail();
    return view('index')
        ->with(compact('user'));


    //
}

这是UserProfileController

 <?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;

 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Auth;

class UserProfileController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */


public function __construct()
{

    $this->middleware('auth');

}


public function index()
{

    $user = Auth::user()->firstOrFail();


    return view('profile.index')
        ->with(compact('user'));
    //
}

路线

 <?php


use Illuminate\Support\Facades\Mail;
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/*Welcome Page routes */


Route::get('/' , function(){
    return view('auth.login');
});


Route::get('user/logout' , function(){
    Auth::logout();
return redirect('/');
});


Route::post('user/do-login' , 'Auth\AuthController@doLogin');

Route::get('user/create' , 'Auth\AuthController@createUser');

/*---------------------------*/


/*Welcome Page AJAX routes*/
Route::get('/checkAvailibility' , 'WelcomePageController@usercheck');







/*---------------------------*/



/*Images Routes*/
Route::post('image/do-upload' ,'GalleryController@doImageUpload');
Route::post('gallery/save' , 'GalleryController@saveGallery');

Route::get('gallery/list' , 'GalleryController@viewGalleryList');
Route::get('gallery/view/{id}' , 'GalleryController@viewGalleryPics');
Route::get('gallery/delete/{id}' , 'GalleryController@deleteGallery');
/*---------------------------*/


/*Event Routes */

Route::post('create/Image-Upload' , 'CreateEventFormController@ImageUpload');
Route::post('create' , 'CreateEventFormController@store');


Route::get('create/event-image' , 'CreateEventFormController@createEventFormImage');
Route::post('create/event-image' , 'CreateEventFormController@UploadEventImg');

Route::get('create' , 'CreateEventFormController@create');

Route::get('event/{id}' , [ 'as' => 'event_list' , 'uses' => 'CreateEventFormController@show']);


/*---------------------------*/




/*-------------- Main Page Routes -------------------*/


Route::get('event/{id}' , [ 'as' => 'event_list' , 'uses' => 'CreateEventFormController@show']);


Route::get('index' ,[ 'as' => 'index' , 'uses' => 'IndexPageController@index']);




/*-------------- END -------------------*/






/*-------------- Main Page Routes -------------------*/


Route::get('profile' , 'UserProfileController@index');
Route::get('profile/{id}' , 'IndexPageController@show');


/*-------------- END -------------------*/









Route::get('/email', function () {

    $data = array(
        'name' => "Learning Laravel",
    );

    Mail::send('emails.test', $data, function ($message) {

        $message->from('yourEmail@domain.com', 'Learning Laravel');

        $message->to('yourEmail@domain.com')->subject('Learning Laravel test email');

    });

    return "Your email has been sent successfully";

});

1 个答案:

答案 0 :(得分:0)

AuthController.php可能存在某些内容。

您是否有机会使用Auth::attempt()进行身份验证?如果是这样的话,我在过去经历过一些问题,在认证之后回复问题导致问题。

另一种可能性是存储会话出错。它可能就像没有保存/写入的会话一样简单。