mafortis
mafortis

Reputation: 7128

Laravel prefix routes redirect

I have admin prefix where url/admin/dashboard is my dashboard view. What I need is to redirect users to url above if they type only url/admin .

This is what I have:

Route::prefix('admin')->group(function () {
  Route::get('dashboard', 'HomeController@index')->name('dashboard'); //works
  Route::get('/', function () {
      return redirect()->route('dashboard');
  }); //doesn't work
});

Upvotes: 4

Views: 4501

Answers (3)

Ganesh
Ganesh

Reputation: 3436

The latest Laravel made it even easier. Define the route for dashboard followed by redirect. Have a look.

Route::get('url/admin/dashboard', 'HomeController@index')->name('dashboard');

Route::redirect('url/admin', 'url/admin/dashboard');

Upvotes: 0

Prince Lionel N'zi
Prince Lionel N'zi

Reputation: 2588

You can do

Route::get('url/admin/{name?}', 'HomeController@index')
    ->where('name', 'dashboard')
    ->name('dashboard');

Or if you want to use the prefix

Route::prefix('admin')->group(function () {
  Route::get('/{name?}', 'HomeController@index')
    ->where('name', 'dashboard')
    ->name('dashboard'); 
});

Upvotes: 0

user9278880
user9278880

Reputation:

You might want to use this:

Route::get('url/admin/dashboard', 'HomeController@index')->name('dashboard');
Route::get('url/admin', function () {
    return redirect('url/admin/dashboard');
});

Upvotes: 2

Related Questions