Laravel Middleware: Where should you put it as a best practice?












0















I was reading the Laravel documentation about middleware and at a certain point it said: "it would be more convenient to specify middleware within your controller's constructor."



I always assigned the middleware to the routes in the routing files because it felt easier to understand which functions were affected by the middleware.



I was wondering if there was a specific reason why the documentation says to put the middleware assignments directly in the Controller constructor or if it was just a matter of preference.










share|improve this question



























    0















    I was reading the Laravel documentation about middleware and at a certain point it said: "it would be more convenient to specify middleware within your controller's constructor."



    I always assigned the middleware to the routes in the routing files because it felt easier to understand which functions were affected by the middleware.



    I was wondering if there was a specific reason why the documentation says to put the middleware assignments directly in the Controller constructor or if it was just a matter of preference.










    share|improve this question

























      0












      0








      0








      I was reading the Laravel documentation about middleware and at a certain point it said: "it would be more convenient to specify middleware within your controller's constructor."



      I always assigned the middleware to the routes in the routing files because it felt easier to understand which functions were affected by the middleware.



      I was wondering if there was a specific reason why the documentation says to put the middleware assignments directly in the Controller constructor or if it was just a matter of preference.










      share|improve this question














      I was reading the Laravel documentation about middleware and at a certain point it said: "it would be more convenient to specify middleware within your controller's constructor."



      I always assigned the middleware to the routes in the routing files because it felt easier to understand which functions were affected by the middleware.



      I was wondering if there was a specific reason why the documentation says to put the middleware assignments directly in the Controller constructor or if it was just a matter of preference.







      laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 16:21









      Matteo PulcranoMatteo Pulcrano

      1




      1
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Its all depends on your own choice.



          I always prefer to use middleware in route as a group, which keeps things centralized and I can find them easily.



          Route::group(['middleware' => ['middleware1']], function () {
          // your routes under middleware1
          });

          Route::group(['middleware' => ['middleware2']], function () {
          // your routes under middleware2
          });





          share|improve this answer































            0














            I think it's a matter of preference, i like to assign the middleware to the route so i don't have to exclude certain methods in the controller plus it's much easier to know which routes use the middleware just by looking at the routes file.






            share|improve this answer































              0














              it is more convenient to specify middleware within your controller's constructor.



              This simply means you have more control over each methods if you use it in your controller's constructor, so that you can specify certain rules in method levels.



              But when you are assigning to the routes you can only control the routes rules.






              share|improve this answer































                0














                Thank you for your question, as mentioned before in most cases it is a decision made by the team you are working with.
                From my own personal experience, I can only explain to you why we choose to use middleware definition in routes file instead of a controller.




                1. In large applications, there are many cases of a dozen controllers.
                  With that being said, there can be cases where you have to change some middleware name. If you define it on route group you would have to change only one line of code, but if you define it in a constructor you would have to go to every controller and change it.


                2. Some companies are using controllers to inject certainly classes into it. There can be a huge amount of injecting and assigning classes in the constructor itself.
                  That's why defining crucial checks should not be happening on this level of code.







                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%2f54269060%2flaravel-middleware-where-should-you-put-it-as-a-best-practice%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  Its all depends on your own choice.



                  I always prefer to use middleware in route as a group, which keeps things centralized and I can find them easily.



                  Route::group(['middleware' => ['middleware1']], function () {
                  // your routes under middleware1
                  });

                  Route::group(['middleware' => ['middleware2']], function () {
                  // your routes under middleware2
                  });





                  share|improve this answer




























                    1














                    Its all depends on your own choice.



                    I always prefer to use middleware in route as a group, which keeps things centralized and I can find them easily.



                    Route::group(['middleware' => ['middleware1']], function () {
                    // your routes under middleware1
                    });

                    Route::group(['middleware' => ['middleware2']], function () {
                    // your routes under middleware2
                    });





                    share|improve this answer


























                      1












                      1








                      1







                      Its all depends on your own choice.



                      I always prefer to use middleware in route as a group, which keeps things centralized and I can find them easily.



                      Route::group(['middleware' => ['middleware1']], function () {
                      // your routes under middleware1
                      });

                      Route::group(['middleware' => ['middleware2']], function () {
                      // your routes under middleware2
                      });





                      share|improve this answer













                      Its all depends on your own choice.



                      I always prefer to use middleware in route as a group, which keeps things centralized and I can find them easily.



                      Route::group(['middleware' => ['middleware1']], function () {
                      // your routes under middleware1
                      });

                      Route::group(['middleware' => ['middleware2']], function () {
                      // your routes under middleware2
                      });






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 19 at 16:47









                      Abdur RahmanAbdur Rahman

                      54110




                      54110

























                          0














                          I think it's a matter of preference, i like to assign the middleware to the route so i don't have to exclude certain methods in the controller plus it's much easier to know which routes use the middleware just by looking at the routes file.






                          share|improve this answer




























                            0














                            I think it's a matter of preference, i like to assign the middleware to the route so i don't have to exclude certain methods in the controller plus it's much easier to know which routes use the middleware just by looking at the routes file.






                            share|improve this answer


























                              0












                              0








                              0







                              I think it's a matter of preference, i like to assign the middleware to the route so i don't have to exclude certain methods in the controller plus it's much easier to know which routes use the middleware just by looking at the routes file.






                              share|improve this answer













                              I think it's a matter of preference, i like to assign the middleware to the route so i don't have to exclude certain methods in the controller plus it's much easier to know which routes use the middleware just by looking at the routes file.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 19 at 16:34









                              Hassan IbrahimHassan Ibrahim

                              413




                              413























                                  0














                                  it is more convenient to specify middleware within your controller's constructor.



                                  This simply means you have more control over each methods if you use it in your controller's constructor, so that you can specify certain rules in method levels.



                                  But when you are assigning to the routes you can only control the routes rules.






                                  share|improve this answer




























                                    0














                                    it is more convenient to specify middleware within your controller's constructor.



                                    This simply means you have more control over each methods if you use it in your controller's constructor, so that you can specify certain rules in method levels.



                                    But when you are assigning to the routes you can only control the routes rules.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      it is more convenient to specify middleware within your controller's constructor.



                                      This simply means you have more control over each methods if you use it in your controller's constructor, so that you can specify certain rules in method levels.



                                      But when you are assigning to the routes you can only control the routes rules.






                                      share|improve this answer













                                      it is more convenient to specify middleware within your controller's constructor.



                                      This simply means you have more control over each methods if you use it in your controller's constructor, so that you can specify certain rules in method levels.



                                      But when you are assigning to the routes you can only control the routes rules.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 19 at 16:39









                                      Fahim UddinFahim Uddin

                                      3031314




                                      3031314























                                          0














                                          Thank you for your question, as mentioned before in most cases it is a decision made by the team you are working with.
                                          From my own personal experience, I can only explain to you why we choose to use middleware definition in routes file instead of a controller.




                                          1. In large applications, there are many cases of a dozen controllers.
                                            With that being said, there can be cases where you have to change some middleware name. If you define it on route group you would have to change only one line of code, but if you define it in a constructor you would have to go to every controller and change it.


                                          2. Some companies are using controllers to inject certainly classes into it. There can be a huge amount of injecting and assigning classes in the constructor itself.
                                            That's why defining crucial checks should not be happening on this level of code.







                                          share|improve this answer




























                                            0














                                            Thank you for your question, as mentioned before in most cases it is a decision made by the team you are working with.
                                            From my own personal experience, I can only explain to you why we choose to use middleware definition in routes file instead of a controller.




                                            1. In large applications, there are many cases of a dozen controllers.
                                              With that being said, there can be cases where you have to change some middleware name. If you define it on route group you would have to change only one line of code, but if you define it in a constructor you would have to go to every controller and change it.


                                            2. Some companies are using controllers to inject certainly classes into it. There can be a huge amount of injecting and assigning classes in the constructor itself.
                                              That's why defining crucial checks should not be happening on this level of code.







                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              Thank you for your question, as mentioned before in most cases it is a decision made by the team you are working with.
                                              From my own personal experience, I can only explain to you why we choose to use middleware definition in routes file instead of a controller.




                                              1. In large applications, there are many cases of a dozen controllers.
                                                With that being said, there can be cases where you have to change some middleware name. If you define it on route group you would have to change only one line of code, but if you define it in a constructor you would have to go to every controller and change it.


                                              2. Some companies are using controllers to inject certainly classes into it. There can be a huge amount of injecting and assigning classes in the constructor itself.
                                                That's why defining crucial checks should not be happening on this level of code.







                                              share|improve this answer













                                              Thank you for your question, as mentioned before in most cases it is a decision made by the team you are working with.
                                              From my own personal experience, I can only explain to you why we choose to use middleware definition in routes file instead of a controller.




                                              1. In large applications, there are many cases of a dozen controllers.
                                                With that being said, there can be cases where you have to change some middleware name. If you define it on route group you would have to change only one line of code, but if you define it in a constructor you would have to go to every controller and change it.


                                              2. Some companies are using controllers to inject certainly classes into it. There can be a huge amount of injecting and assigning classes in the constructor itself.
                                                That's why defining crucial checks should not be happening on this level of code.








                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jan 19 at 21:48









                                              FondueBugFondueBug

                                              1




                                              1






























                                                  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%2f54269060%2flaravel-middleware-where-should-you-put-it-as-a-best-practice%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

                                                  Liquibase includeAll doesn't find base path

                                                  How to use setInterval in EJS file?

                                                  Petrus Granier-Deferre