Support the ongoing development of Laravel.io →
Authentication Security
Last updated 1 year ago.
0

First of all, you just need to mark up your routes slightly differently. Use an array instead of a string:

Route::get('logout', array('before' => 'auth', 'uses' => 'UsersController@logout'));

And to use Route groups, just wrap them like this

Route::group(array('before' => 'auth'), function(){
  Route::get('owners', array('uses' => 'PownersController@ownerlist'));
  // more routes here...
});

Laravel Docs: Route Groups

Last updated 1 year ago.
0

You said:

Route::group(array('before' => 'auth'), function(){
  Route::get('owners', array('uses' => 'PownersController@ownerlist'));
  // more routes here...
});

So just put my list of routes in the function but change the format to

Route::get('owners', array('uses' => 'PownersController@ownerlist'));

Does the csrf token work like this, in a group? And thanks for the reply, your reply makes more sense than anything I've read so far anywhere.

In fact you put the link to docs which state:

Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter
    });

    Route::get('user/profile', function()
    {
        // Has Auth Filter
    });
});

Which doesn't make any sense. Your answer was clear and easy to follow. In the docs, it's not even a route! Oh, just for grins, would the technique I did:

if (Auth::check())
{
//list of routes
}

work, or not. is the technique ok or not? I'm using your answer, but I'm just curious.

Last updated 1 year ago.
0
Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter
    });

    Route::get('user/profile', function()
    {
        // Has Auth Filter
    });
});

How does this not make sense?

Think of it as a wrapper, any route inside the "group" wrapper will have the auth filter applied to it.

It is the same as adding the "auth" filter to each of the routes, it just saves some duplicate code.

You can do the same for paths, like the /user/profile or /admin with prefix.

As example from one of my admin modules,


/*
 * ADMIN ROUTES
 */
Route::group(array('prefix' => 'admin', 'before' => 'admin-auth'), function()
{
	
	// handles www.somesite.com/admin/
	Route::get('/', array('as' => 'admin-dashboard', 'uses' => 'Wpb\AdminModule\Controllers\IndexController@getIndex'));

	// pages
	Route::group(array('prefix' => 'pages'), function()
	{
		// handles www.somesite.com/admin/pages/
        	Route::get('/', array('as' => 'admin-pages', 'uses' => 'Wpb\AdminModule\Controllers\PageController@getIndex'));

		// handles www.somesite.com/admin/pages/edit/{id}		
		Route::get('/edit/{id?}', array('as' => 'admin-pages-edit', 'uses' => 'Wpb\AdminModule\Controllers\PageController@getEdit'));

	}   

	... there are a lot more in my route file
}

All of the above have the filter admin-auth, and must be in the /admin path on the site.

if (Auth::check())
{
//list of routes
}

This should work, if the Auth::check() returns true then the routes inside will the if statement will be executed and the routes added to the router.

The only problem would be if you try to reference any of the routes, for example - creating a link using the url helper, and the routes are not there the router would not know the route exists and likely error.

Several people are doing this, or something similar, for when their routes being loaded get to large. I haven't implemented it yet on my code, mostly because of the url helper usage.

Hope that helps explain is some.

Last updated 1 year ago.
0
Route::group(array('before' => 'auth'), function(){
  Route::get('owners', array('uses' => 'PownersController@ownerlist'));
});

The above from meigwilym makes sense, a concrete example: Route::get('owners', array('uses' => 'PownersController@ownerlist')); Actually shows how a route is actually being used. BELOW makes NO sense:

Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter   ////THIS IS TELLING ME NOTHING ON USAGE
    });

Not a good example how does // Has Auth Filter explain anything, remember meigwilym's example shows usage, where as // Has Auth Filter makes no sense of usage. Some folks are new to laravels routing and need usage examples (actual code examples). Thanks for your response. So far I like laravel, but the manual is vague on some things. Also I like regular plain jane routing i.e.

Route::get('owners', array('uses' => 'PownersController@ownerlist'));

NOT

Route::get('owners', array('uses' => 'PownersController@ownerlist'));
function this
function that
bla bla function of another
if the chicken clucks function
well you catch my drift 

JUST

Route::get('the uri wording', array('uses' => 'the controller to use@the controller method'));
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jimgwhit jimgwhit Joined 13 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.