0
votes

I'm new to Laravel 5, I just want to know how can I protect specific routes? I'm using the default Authenticate Middleware to protect my routes which is only accessible if you're logged in. So i have this.

Route::group(['middleware' => 'App\Http\Middleware\Authenticate'], function()    /* Admin only Routes*/
{
     Route::get('/brk/datalist', 'BRKDailyMailsController@datalist');
});

How can I protect the /auth/register only? without affecting the others like login?

Outside my route group I have this by default.

Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);

I only want logged in users to access default register page. My logged in users is all admin.

2

2 Answers

0
votes

You can set middleware for specific route like this:

Route::any('register', [
    'as' => 'register', 
    'middleware' => 'auth',
    'uses' => 'AuthController@anyRegister'
]);

This way only logged in users will be able to "use" the /register page.

It is named "register" too so you can use it in your templates, redirects etc. by name.

And if it "passes" the middleware (user is logged in) the anyRegister action from AuthController will be executed.

Feel free to modify this as you need, it's just an example of how can this be achieved.

0
votes

You can try like this

Route::group(['middleware' => 'auth'], function()
{
    Route::get('/brk/datalist', 'BRKDailyMailsController@datalist');
});

Or you can use construct on first line in every controller

public function __construct()
{
    $this->middleware('auth');
}