Adding multiple middleware to Laravel route












13















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










share|improve this question





























    13















    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










    share|improve this question



























      13












      13








      13


      2






      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 24 '18 at 5:59









      Prashant Pokhriyal

      2,23841624




      2,23841624










      asked Nov 26 '16 at 20:36









      user1032531user1032531

      11k35137245




      11k35137245
























          2 Answers
          2






          active

          oldest

          votes


















          25














          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
          });





          share|improve this answer


























          • 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






          • 3





            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











          • 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



















          1














          You may also assign multiple middleware to the route:



          Route::get('/', function () {
          //
          })->middleware('first', 'second');


          Reference






          share|improve this answer

























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            25














            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
            });





            share|improve this answer


























            • 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






            • 3





              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











            • 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
















            25














            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
            });





            share|improve this answer


























            • 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






            • 3





              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











            • 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














            25












            25








            25







            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
            });





            share|improve this answer















            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
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 25 '18 at 7:38

























            answered Nov 26 '16 at 21:20









            krlvkrlv

            1,261610




            1,261610













            • 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






            • 3





              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











            • 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



















            • 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






            • 3





              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











            • 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

















            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













            1














            You may also assign multiple middleware to the route:



            Route::get('/', function () {
            //
            })->middleware('first', 'second');


            Reference






            share|improve this answer






























              1














              You may also assign multiple middleware to the route:



              Route::get('/', function () {
              //
              })->middleware('first', 'second');


              Reference






              share|improve this answer




























                1












                1








                1







                You may also assign multiple middleware to the route:



                Route::get('/', function () {
                //
                })->middleware('first', 'second');


                Reference






                share|improve this answer















                You may also assign multiple middleware to the route:



                Route::get('/', function () {
                //
                })->middleware('first', 'second');


                Reference







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python