2016-09-15 116 views
2

我在Laravel做了一个登录/注册页面,它工作正常,但我想通过允许经过身份验证的用户单独访问该URL来保护路由。Laravel 5.3路由保护问题

这是我web.php:

Route::get('/', [ 
    'uses' => '[email protected]', 
    'as' => 'login' 
]); 

Route::get('/signup', [ 
    'uses' => '[email protected]', 
    'as' => 'signup' 
]); 

Route::get('/logout', [ 
     'uses' => '[email protected]', 
     'as' => 'logout' 
]); 

Route::group(['prefix' => 'app'], function(){ 

    Route::post('/newuser', [ 
     'uses' => '[email protected]', 
     'as' => 'submitsignup' 
    ]); 

    Route::post('/submitsignup', [ 
      'uses' => '[email protected]', 
      'as' => 'submitlogin' 
    ]); 

    Route::get('/home', [ 
      'uses' => '[email protected]', 
      'as' => 'dashboard' 
    ])->middleware('auth'); 

    // I also tried 'middleware' => 'auth', ends in same thing 

}); 

在我UserController.php:

public function getSignup(){ 
    $organizations = Organizations::all()->where('deleted', '0')->all(); 
    return view('pages.signup', ['organizations' => $organizations]); 
} 

public function getLogin(){ 
    return view('pages.login'); 
} 

public function getDashboard(){ 
    return view('pages.dashboard'); 
} 

public function getLogout(){ 
    Auth::logout(); 
    return redirect()->route('login'); 
} 

public function postSubmitSignup(Request $request){ 

    $newuser = new User(); 

    $newuser->firstname = $request['firstname']; 
    $newuser->lastname = $request['lastname']; 
    $newuser->username = $request['username']; 
    $newuser->email = $request['email']; 
    $newuser->password = bcrypt($request['password']); 
    $newuser->passwordhint = $request['passwordhint']; 
    $newuser->organization = $request['organization']; 
    $newuser->location = $request['location']; 
    $newuser->phone = $request['phone']; 
    $newuser->signupnote = $request['remarks']; 

    $newuser->save(); 

    return redirect()->route('login'); 
} 

public function postSubmitLogin(Request $request){ 
    if(Auth::attempt(["username" => $request['username'], "password" => $request['password']])){ 
     return redirect()->route('dashboard'); 
    } 

    session()->flash('invalid', 'Bad Credentials'); 
    return redirect()->back()->withInput(); 

} 

,当我尝试使用有效证书登录,我收到以下错误消息和网址似乎是http://website.com/login但登录页面位于http://website.com/

Sorry, the page you are looking for could not be found. 
1/1 NotFoundHttpException in RouteCollection.php line 161: 

    in RouteCollection.php line 161 
    at RouteCollection->match(object(Request)) in Router.php line 780 
    at Router->findRoute(object(Request)) in Router.php line 610 
    at Router->dispatchToRoute(object(Request)) in Router.php line 596 
    at Router->dispatch(object(Request)) in Kernel.php line 267 
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53 

当我尝试直接访问仪表板网址时,出现相同的错误。我应该如何正确地做,如果有人能解释为什么会发生这种情况,那该多好。

+0

你只是想保护路线或什么? –

+0

如果你想保护一些特定的路由,只需在auth中间件中进行分组即可。 'Route :: group(['middleware'=>'auth'],function(){ //您的路线 }' –

+0

所以他们不需要在网络下?我看到教程把它们放在网络和做这样我所做的。另一件事是什么样子,在5.3路由默认情况下在网上。 – JackSlayer94

回答

1

你需要将你的路线如下图所示

Route::group(['middleware' => 'auth'], function(){ 

    Route::get('/logout', [ 
     'uses' => '[email protected]', 
     'as' => 'logout' 
    ]); 


// and your other routes which you wanna protect 
} 

现在注销航线等航线,您将添加在将只有通过认证的用户访问,到底是谁在登录简单的话用户。

+0

@liftikhar uddin no still does not work。我得到同样的东西。这里是我的整个项目:GitHub上(https://github.com/yeshwanthvshenoy/De-Bug)。你能指出什么是错的?我被困在一些非常小的东西里。 – JackSlayer94

+0

https://gist.github.com/iftikharuddin/9ef222dafb2e47d8e715faefbf89c03c试试这个,现在一切是在小组中间件AUTH将只对登录用户 –

+0

不,这并不能解决问题访问。我得到了上面提到的同样的错误。 – JackSlayer94