4

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
});
mafortis
  • 6,750
  • 23
  • 130
  • 288

3 Answers3

2

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');
});
  • they are under `admin` prefix normally when i say `/` it mean prefix itself. If i say `url/admin` will be `url/admin/admin`. – mafortis Jan 28 '18 at 08:45
0

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'); 
});
Prince Lionel N'zi
  • 2,510
  • 2
  • 12
  • 27
0

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');
Ganesh
  • 3,128
  • 2
  • 17
  • 27
  • Dude, this post was published 3 years ago now after 3 years you're coming and talk about latest version of laravel?!!! :D – mafortis Mar 31 '21 at 16:05