Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 919
pinkal vansia Avatar asked Jun 16 '15 14:06

pinkal vansia


People also ask

What are the two main routing files found in Laravel?

All Laravel routes are defined in route files found within the routes directory. The application's App\Providers\RouteServiceProvider automatically stacks these files. And the routes/web. php file defines the routes for your web interface.

How can I get current route in Laravel?

Accessing The Current RouteThe Route::current() method will return the route handling the current HTTP request, allowing you to inspect the full Illuminate\Routing\Route instance: $route = Route::current(); $name = $route->getName(); $actionName = $route->getActionName();

What does a namespace Route Group allow?

Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route. Shared attributes are specified in an array format as the first parameter to the Route::group method.


1 Answers

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.

like image 152
Alex Kyriakidis Avatar answered Oct 09 '22 15:10

Alex Kyriakidis