2016-10-17 65 views
1

我剛剛安裝了laravel 5.2,並且我創建了auth註冊,登錄和重置密碼,但是現在我想創建一個我的項目的索引,其中所有用戶(也都未登錄)都可以訪問。我試圖創建Laravel 5.2 - 中間件認證

Route :: get('/',HomeController @ home');

但是這個視圖只對用戶登錄纔有效。

MY ROUTES

Route::auth(); 
Route::get('/home', '[email protected]'); 
// POST - FORM CREA 
Route::get('/crea-regalo', '[email protected]'); 
Route::post('/crea-regalo', '[email protected]'); 
// LISTA ANNUNCI PRINCIPALE 
Route::get('/', '[email protected]'); 

MY HOME控制器

class HomeController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 

    /** 
    * Show the application dashboard. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $posts = Post::orderBy('id','DESC'); 
     return view('home', compact('posts')); 
    } 

    public function home() 
    { 
     $posts = Post::all(); 
     return view('index', compact('posts')); 
    } 
} 

如何創建視圖,所有用戶都可以訪問路線?

謝謝你的幫助!

回答

1

喜寫單獨的控制器來訪問頁面,都是因爲你已經在構造器

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

類似像

class GuestController extends Controller 
{ 

    public function __construct() 
    { 

    } 


    public function home() 
    { 
     $posts = Post::all(); 
     return view('index', compact('posts')); 
    } 
} 

書面權威性中間件在路線

Route::get('/home', '[email protected]'); 

或其他你能像這樣做

$this->middleware('auth', ['except' => ['home']]); 

這將能夠訪問主頁所有。在構造函數中添加此

public function __construct() 
{ 
    $this->middleware('auth', ['except' => ['home']]); 
} 
+0

yup!謝謝!我將創建另一個控制器,是最好的選擇! –

2

將要允許中間件權威性只有經過身份驗證的用戶的路線如下:

Route::group(['middleware' => ['auth']], function() { 
    //your routes  
}) 

對於那些所有用戶都可以訪問的路線,將其放在上面的組中。

+0

也謝謝你! –