2016-10-17 12 views
1

私はlaravel 5.2をインストールしましたが、auth register、login、reset passwordを作成しましたが、今はすべてのユーザ(ログインしていないユーザ)がアクセスできるプロジェクトのインデックスを作成します。私は作成しようとしました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のCONTROLLER

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

どのように私はすべてのユーザーがアクセスできるビューのルートを作成することができますか?

ありがとうございました!あなたはルート

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

class GuestController extends Controller 
{ 

    public function __construct() 
    { 

    } 


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

よう

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

同様のコンストラクタで認証ミドルウェアを書かれているので、

答えて

1

こんにちは、すべてのページにアクセスするために、別のコントローラを書くか、他のことができますこのようにする

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

これはあなたのコンストラクタ.INすべてのホーム・ページにアクセスすることができ、次のようにこの

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

yup!ありがとうございました!私は別のコントローラを作成する、最良の選択です! –

2

あなたはミドルウェアAUTHで認証されたユーザのみを許可するそれらのルートを入れて追加します。

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

そして、すべてのユーザーがアクセスできるルートの場合は、上記のグループを外に出してください。

+0

ありがとうございます! –

関連する問題