Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 route not defined redirect

need to ask about error, when I tried to redirect my logout

This is UserController:

public function logout()
{

    Auth::logout();
    return redirect()->route('auth/login');
}

This is my routes:

Route::get('/', function () {
    return view('welcome');
});
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

Route::get('testing', 'UserController@test');
Route::get('logout', 'UserController@logout');

I think, taht everything is ok on routes and I defined login properly (like a Laravel documentation)

but it still take this error:

InvalidArgumentException in UrlGenerator.php line 306:
Route [auth/login] not defined.

Can you please what is wrong? Did I make any mistake?

Thanks a lot, have a nice day!

like image 850
Mario Avatar asked Oct 29 '25 05:10

Mario


2 Answers

The route method expects a name, not a URI. So you need to name your route.

Like this:

Route::get('auth/login', ['as' => 'login', 'uses' => 'Auth\AuthController@getLogin']);

Or:

Route::get('auth/login', 'Auth\AuthController@getLogin')->name('login');

Now you can call return redirect()->route('login');

See the docs for Named Routes.


Alternatively, you can just provide the URI in the redirect method like this:

return redirect('auth/login');

Though this would break if you ever change this endpoint. I'd recommend naming your routes and using those in your code.

like image 108
jszobody Avatar answered Oct 30 '25 22:10

jszobody


change your return redirect()->route('auth/login'); to

return redirect('auth/login');

or return Redirect::to('auth/login');

like image 22
LF00 Avatar answered Oct 30 '25 21:10

LF00