第一次尝试与Laravel一起玩。我尝试访问时收到以下错误:
InvalidArgumentException
路线[仪表板]未定义。
路由/ web.php
Route::get('/', ['as' => '/', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController@postLogin']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController@getLogout']);
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@dashboard']);
});
LoginController.php
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web'))
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt([
'username' => $request->username,
'password' => $request->password,
'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
答案 0 :(得分:0)
试试这个:
Route::get('/dashboard','DashboardController@dashboard')->name('dashboard');
答案 1 :(得分:0)
当您使用它来命名路线时,因此您添加了两个名称dashbbaorddashboard,因为您使用了as
和name
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['uses' => 'DashboardController@dashboard'])->name('dashboard');
});
这将有效
答案 2 :(得分:0)
as
赞name()
。您应该使用以下两种之一:
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', 'DashboardController@dashboard')->name('dashboard');
});
或
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', [
'as' => 'dashboard',
'uses' => 'DashboardController@dashboard']);
});
之后,您使用php artisan route:clear
最后,您可以使用php artisan route:list
列出所有路线和动作绑定