Adding multiple middleware to Laravel route
Per laravel doc, I can add the auth
middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
php laravel
add a comment |
Per laravel doc, I can add the auth
middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
php laravel
add a comment |
Per laravel doc, I can add the auth
middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
php laravel
Per laravel doc, I can add the auth
middleware as follows:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
I've also seen middleware added as follows:
Route::group(['middleware' => ['web']], function() {
// Uses all Middleware $middlewareGroups['web'] located in /app/Http/kernel.php?
Route::resource('blog','BlogController'); //Make a CRUD controller
});
How can I do both?
PS. Any comments providing insight on what the bottom four lines of code are doing would be appreciated
php laravel
php laravel
edited Dec 24 '18 at 5:59
Prashant Pokhriyal
2,23841624
2,23841624
asked Nov 26 '16 at 20:36
user1032531user1032531
11k35137245
11k35137245
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
Ah, soRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same function?
– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includesRoute::group(...);
so I would apply to a group.
– user1032531
Nov 26 '16 at 21:36
3
1. YesRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same
– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're usingRoute::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route
– krlv
Nov 26 '16 at 22:50
add a comment |
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40822882%2fadding-multiple-middleware-to-laravel-route%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
Ah, soRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same function?
– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includesRoute::group(...);
so I would apply to a group.
– user1032531
Nov 26 '16 at 21:36
3
1. YesRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same
– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're usingRoute::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route
– krlv
Nov 26 '16 at 22:50
add a comment |
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
Ah, soRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same function?
– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includesRoute::group(...);
so I would apply to a group.
– user1032531
Nov 26 '16 at 21:36
3
1. YesRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same
– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're usingRoute::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route
– krlv
Nov 26 '16 at 22:50
add a comment |
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.
To use both (single middleware & middleware group) you can try this:
Route::group(['middleware' => ['auth', 'web']], function() {
// uses 'auth' middleware plus all middleware from $middlewareGroups['web']
Route::resource('blog','BlogController'); //Make a CRUD controller
});
edited Dec 25 '18 at 7:38
answered Nov 26 '16 at 21:20
krlvkrlv
1,261610
1,261610
Ah, soRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same function?
– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includesRoute::group(...);
so I would apply to a group.
– user1032531
Nov 26 '16 at 21:36
3
1. YesRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same
– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're usingRoute::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route
– krlv
Nov 26 '16 at 22:50
add a comment |
Ah, soRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same function?
– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includesRoute::group(...);
so I would apply to a group.
– user1032531
Nov 26 '16 at 21:36
3
1. YesRoute::group(['middleware' => ['web']], function() {});
andRoute::group(['middleware' => 'web'], function() {});
performs the same
– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're usingRoute::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route
– krlv
Nov 26 '16 at 22:50
Ah, so
Route::group(['middleware' => ['web']], function() {});
and Route::group(['middleware' => 'web'], function() {});
performs the same function?– user1032531
Nov 26 '16 at 21:33
Ah, so
Route::group(['middleware' => ['web']], function() {});
and Route::group(['middleware' => 'web'], function() {});
performs the same function?– user1032531
Nov 26 '16 at 21:33
Also, so the first code snippet isn't using group middleware? The script includes
Route::group(...);
so I would apply to a group.– user1032531
Nov 26 '16 at 21:36
Also, so the first code snippet isn't using group middleware? The script includes
Route::group(...);
so I would apply to a group.– user1032531
Nov 26 '16 at 21:36
3
3
1. Yes
Route::group(['middleware' => ['web']], function() {});
and Route::group(['middleware' => 'web'], function() {});
performs the same– krlv
Nov 26 '16 at 22:47
1. Yes
Route::group(['middleware' => ['web']], function() {});
and Route::group(['middleware' => 'web'], function() {});
performs the same– krlv
Nov 26 '16 at 22:47
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
2. The first code snippet is using 'auth' middleware, it's built-in Laravel middleware, not a group middleware
– krlv
Nov 26 '16 at 22:49
You're using
Route::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route– krlv
Nov 26 '16 at 22:50
You're using
Route::group(...)
in both cases so in both cases it will be applied to a route group, not to a single route– krlv
Nov 26 '16 at 22:50
add a comment |
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
add a comment |
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
add a comment |
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
You may also assign multiple middleware to the route:
Route::get('/', function () {
//
})->middleware('first', 'second');
Reference
edited Dec 24 '18 at 6:01
Prashant Pokhriyal
2,23841624
2,23841624
answered Aug 9 '18 at 14:06
Anandan KAnandan K
1269
1269
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40822882%2fadding-multiple-middleware-to-laravel-route%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown