Laravel 5 Route Group and Basic(/) GET Route within group

4.4k Views Asked by At

I am working on a Laravel 5.0 Web application with Admin panel. I am facing an issue with Routes. I have Grouped Admin Routes like below,

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['user.admin']], function () {
    Route::get('login', [
        'as' => 'admin.login',
        'uses' => 'AuthController@getLogin'
    ]);
    Route::get('logout', [
        'as' => 'admin.login',
        'uses' => 'AuthController@getLogout'
    ]);
    Route::post('login', 'AuthController@postLogin');
});

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['user.admin','auth', 'admin.acl']], function () {

    Route::get('dashboard', [
        'as'         => 'admin.dashboard',
        'uses'       => 'DashboardController@index',
        'permission' => 'admin_dashboard'
    ]);

    //Image Handler
    Route::get('images/{size}/{name?}',[
        'as'   => 'admin.images',
        'uses' => 'ImagesController@images'
    ]);

    Route::resource('user', 'UsersController');

    ........   

});

Things are working fine. I can use following without any problem,

http://domain.com/admin/dashboard
http://domain.com/admin/login

But I want

http://domain.com/admin

to display login page or redirect to

http://domain.com/admin/login

so I changed my first group to following,

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['user.admin']], function () {

    Route::get('/', [        
        'uses' => 'AuthController@getLogin'
    ]);
    Route::get('login', [
        'as' => 'admin.login',
        'uses' => 'AuthController@getLogin'
    ]);
    Route::get('logout', [
        'as' => 'admin.login',
        'uses' => 'AuthController@getLogout'
    ]);
    Route::post('login', 'AuthController@postLogin');
});

Now When I access

http://domain.com/admin

I get 'This webpage has a redirect loop' in chrome. Is it possible in Route group? if not how to do this with .htaccess?

UPDATE

Below is the handle method of a Middleware user.admin. Which does nothing but changes underlying model for authentication.

    public function handle($request, Closure $next)
    {
        \Config::set('auth.table', 'admins');
        \Config::set('auth.model', 'App\DB\Admin\Admin');

        \Config::set('session.cookie', 'admin_session');
        \Config::set('session.path', '/admin/');

        return $next($request);
    }

UPDATE

This is amazing, following works

http://domain.com/index.php/admin

I have not touched default .htaccess supplied by laravel 5.0, which is below,

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I have almost 60-70 routes, and all of them works without index.php accept in above scenario I needed index.php.

3

There are 3 best solutions below

1
On

You can have that without any prefix, simply add a route 'admin' that does the redirect:

// responds to http://domain.com/admin
Route::get('admin', function(){
  if (//User not authenticated)
    return redirect()->to('admin/login');
  else
    //show admin;
});
0
On

The problem is that 'user.admin' middleware is always running, even on '/admin/login'. So when you access '/admin' you are redirected to '/admin/login' and then the middleware redirects you again to '/admin/login', and this happens forever. That's the reason you get 'This webpage has a redirect loop'.

In order to make it work you have to exclude 'admin/login' from using your 'user.admin' middleware.

0
On

The problem can be caused by a folder named admin in the public directory.