Prevent passing route prefix as the first parameter to child routes in Laravel 5












2















How can I prevent the Route prefix from being passed to all the child routes controller functions as the first parameter? It is messing with the $id parameter of my child routes!



Route::prefix('{locale?}')->middleware(['locale.default', 'locale'])->group(function() {
Route::get('/car/{id}', [
'uses' => 'CarController@getCar',
'as' => 'car',
'middleware' => 'auth'
])->where(['id' => '[0-9]+']);
});


In CarController's getCar() function, the id parameter is getting the locale (en, fr, it) instead of the id of the car being passed.



public function getCar($id)
{
$car = Car::where('id', $id)->firstOrFail();
}


To fix this, I must do:



public function getCar($locale, $id)
{
$car = Car::where('id', $id)->firstOrFail();
}


Is there a way to simply prevent the passing of $locale to the child route's controller functions so that I don't have to add $locale as the first parameter to every function?



If I change the getCar() parameter from $id to $request of type Request, I can do this and it works. Is this a good solution?



public function getCar(Request $request)
{
$car = Car::where('id', $request->id)->firstOrFail();
}


The Laravel 5.6 docs seems to say that this can be done:
https://laravel.com/docs/5.6/requests




Accessing The Request Via Route Closures



You may also type-hint the IlluminateHttpRequest class on a route
Closure. The service container will automatically inject the incoming
request into the Closure when it is executed:



use IlluminateHttpRequest;



Route::get('/', function (Request $request) {
//... });




Can anyone confirm this please? Thanks!










share|improve this question





























    2















    How can I prevent the Route prefix from being passed to all the child routes controller functions as the first parameter? It is messing with the $id parameter of my child routes!



    Route::prefix('{locale?}')->middleware(['locale.default', 'locale'])->group(function() {
    Route::get('/car/{id}', [
    'uses' => 'CarController@getCar',
    'as' => 'car',
    'middleware' => 'auth'
    ])->where(['id' => '[0-9]+']);
    });


    In CarController's getCar() function, the id parameter is getting the locale (en, fr, it) instead of the id of the car being passed.



    public function getCar($id)
    {
    $car = Car::where('id', $id)->firstOrFail();
    }


    To fix this, I must do:



    public function getCar($locale, $id)
    {
    $car = Car::where('id', $id)->firstOrFail();
    }


    Is there a way to simply prevent the passing of $locale to the child route's controller functions so that I don't have to add $locale as the first parameter to every function?



    If I change the getCar() parameter from $id to $request of type Request, I can do this and it works. Is this a good solution?



    public function getCar(Request $request)
    {
    $car = Car::where('id', $request->id)->firstOrFail();
    }


    The Laravel 5.6 docs seems to say that this can be done:
    https://laravel.com/docs/5.6/requests




    Accessing The Request Via Route Closures



    You may also type-hint the IlluminateHttpRequest class on a route
    Closure. The service container will automatically inject the incoming
    request into the Closure when it is executed:



    use IlluminateHttpRequest;



    Route::get('/', function (Request $request) {
    //... });




    Can anyone confirm this please? Thanks!










    share|improve this question



























      2












      2








      2








      How can I prevent the Route prefix from being passed to all the child routes controller functions as the first parameter? It is messing with the $id parameter of my child routes!



      Route::prefix('{locale?}')->middleware(['locale.default', 'locale'])->group(function() {
      Route::get('/car/{id}', [
      'uses' => 'CarController@getCar',
      'as' => 'car',
      'middleware' => 'auth'
      ])->where(['id' => '[0-9]+']);
      });


      In CarController's getCar() function, the id parameter is getting the locale (en, fr, it) instead of the id of the car being passed.



      public function getCar($id)
      {
      $car = Car::where('id', $id)->firstOrFail();
      }


      To fix this, I must do:



      public function getCar($locale, $id)
      {
      $car = Car::where('id', $id)->firstOrFail();
      }


      Is there a way to simply prevent the passing of $locale to the child route's controller functions so that I don't have to add $locale as the first parameter to every function?



      If I change the getCar() parameter from $id to $request of type Request, I can do this and it works. Is this a good solution?



      public function getCar(Request $request)
      {
      $car = Car::where('id', $request->id)->firstOrFail();
      }


      The Laravel 5.6 docs seems to say that this can be done:
      https://laravel.com/docs/5.6/requests




      Accessing The Request Via Route Closures



      You may also type-hint the IlluminateHttpRequest class on a route
      Closure. The service container will automatically inject the incoming
      request into the Closure when it is executed:



      use IlluminateHttpRequest;



      Route::get('/', function (Request $request) {
      //... });




      Can anyone confirm this please? Thanks!










      share|improve this question
















      How can I prevent the Route prefix from being passed to all the child routes controller functions as the first parameter? It is messing with the $id parameter of my child routes!



      Route::prefix('{locale?}')->middleware(['locale.default', 'locale'])->group(function() {
      Route::get('/car/{id}', [
      'uses' => 'CarController@getCar',
      'as' => 'car',
      'middleware' => 'auth'
      ])->where(['id' => '[0-9]+']);
      });


      In CarController's getCar() function, the id parameter is getting the locale (en, fr, it) instead of the id of the car being passed.



      public function getCar($id)
      {
      $car = Car::where('id', $id)->firstOrFail();
      }


      To fix this, I must do:



      public function getCar($locale, $id)
      {
      $car = Car::where('id', $id)->firstOrFail();
      }


      Is there a way to simply prevent the passing of $locale to the child route's controller functions so that I don't have to add $locale as the first parameter to every function?



      If I change the getCar() parameter from $id to $request of type Request, I can do this and it works. Is this a good solution?



      public function getCar(Request $request)
      {
      $car = Car::where('id', $request->id)->firstOrFail();
      }


      The Laravel 5.6 docs seems to say that this can be done:
      https://laravel.com/docs/5.6/requests




      Accessing The Request Via Route Closures



      You may also type-hint the IlluminateHttpRequest class on a route
      Closure. The service container will automatically inject the incoming
      request into the Closure when it is executed:



      use IlluminateHttpRequest;



      Route::get('/', function (Request $request) {
      //... });




      Can anyone confirm this please? Thanks!







      php laravel laravel-5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 4 '18 at 21:41







      stevetronix

















      asked Mar 4 '18 at 21:00









      stevetronixstevetronix

      4891920




      4891920
























          3 Answers
          3






          active

          oldest

          votes


















          1














          It would be wiser to use service provider for setting up the locale.



          This article should guide you through the architecture for the multi-language and Laravel.



          https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website



          All the best.






          share|improve this answer
























          • Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

            – stevetronix
            Mar 4 '18 at 21:15



















          1














          Using a middleware, you simply need to add this line to the handler:



          $request->route()->forgetParameter('prefix');





          share|improve this answer































            0














            public function callAction($method, $parameters)
            {
            unset($parameters['prefix']);

            return parent::callAction($method, $parameters);
            }


            This should be called in your controller.



            https://laracasts.com/discuss/channels/laravel/ignoring-few-route-parameters






            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%2f49100429%2fprevent-passing-route-prefix-as-the-first-parameter-to-child-routes-in-laravel-5%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              It would be wiser to use service provider for setting up the locale.



              This article should guide you through the architecture for the multi-language and Laravel.



              https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website



              All the best.






              share|improve this answer
























              • Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

                – stevetronix
                Mar 4 '18 at 21:15
















              1














              It would be wiser to use service provider for setting up the locale.



              This article should guide you through the architecture for the multi-language and Laravel.



              https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website



              All the best.






              share|improve this answer
























              • Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

                – stevetronix
                Mar 4 '18 at 21:15














              1












              1








              1







              It would be wiser to use service provider for setting up the locale.



              This article should guide you through the architecture for the multi-language and Laravel.



              https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website



              All the best.






              share|improve this answer













              It would be wiser to use service provider for setting up the locale.



              This article should guide you through the architecture for the multi-language and Laravel.



              https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website



              All the best.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 4 '18 at 21:10









              Kavan PancholiKavan Pancholi

              376214




              376214













              • Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

                – stevetronix
                Mar 4 '18 at 21:15



















              • Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

                – stevetronix
                Mar 4 '18 at 21:15

















              Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

              – stevetronix
              Mar 4 '18 at 21:15





              Thanks for replying. This is exactly what I did. As can be seen in my code, I have 2 custom Middleware for handling the locale and setting the default local.

              – stevetronix
              Mar 4 '18 at 21:15













              1














              Using a middleware, you simply need to add this line to the handler:



              $request->route()->forgetParameter('prefix');





              share|improve this answer




























                1














                Using a middleware, you simply need to add this line to the handler:



                $request->route()->forgetParameter('prefix');





                share|improve this answer


























                  1












                  1








                  1







                  Using a middleware, you simply need to add this line to the handler:



                  $request->route()->forgetParameter('prefix');





                  share|improve this answer













                  Using a middleware, you simply need to add this line to the handler:



                  $request->route()->forgetParameter('prefix');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 20:45









                  mytunymytuny

                  7616




                  7616























                      0














                      public function callAction($method, $parameters)
                      {
                      unset($parameters['prefix']);

                      return parent::callAction($method, $parameters);
                      }


                      This should be called in your controller.



                      https://laracasts.com/discuss/channels/laravel/ignoring-few-route-parameters






                      share|improve this answer




























                        0














                        public function callAction($method, $parameters)
                        {
                        unset($parameters['prefix']);

                        return parent::callAction($method, $parameters);
                        }


                        This should be called in your controller.



                        https://laracasts.com/discuss/channels/laravel/ignoring-few-route-parameters






                        share|improve this answer


























                          0












                          0








                          0







                          public function callAction($method, $parameters)
                          {
                          unset($parameters['prefix']);

                          return parent::callAction($method, $parameters);
                          }


                          This should be called in your controller.



                          https://laracasts.com/discuss/channels/laravel/ignoring-few-route-parameters






                          share|improve this answer













                          public function callAction($method, $parameters)
                          {
                          unset($parameters['prefix']);

                          return parent::callAction($method, $parameters);
                          }


                          This should be called in your controller.



                          https://laracasts.com/discuss/channels/laravel/ignoring-few-route-parameters







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 19 '18 at 16:45









                          Chris StryczynskiChris Stryczynski

                          4,36153276




                          4,36153276






























                              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%2f49100429%2fprevent-passing-route-prefix-as-the-first-parameter-to-child-routes-in-laravel-5%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

                              Florida Star v. B. J. F.

                              Error while running script in elastic search , gateway timeout

                              Adding quotations to stringified JSON object values