Prevent passing route prefix as the first parameter to child routes in Laravel 5
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
add a comment |
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
add a comment |
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
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
php laravel laravel-5
edited Mar 4 '18 at 21:41
stevetronix
asked Mar 4 '18 at 21:00
stevetronixstevetronix
4891920
4891920
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
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
add a comment |
Using a middleware, you simply need to add this line to the handler:
$request->route()->forgetParameter('prefix');
add a comment |
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
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%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Using a middleware, you simply need to add this line to the handler:
$request->route()->forgetParameter('prefix');
add a comment |
Using a middleware, you simply need to add this line to the handler:
$request->route()->forgetParameter('prefix');
add a comment |
Using a middleware, you simply need to add this line to the handler:
$request->route()->forgetParameter('prefix');
Using a middleware, you simply need to add this line to the handler:
$request->route()->forgetParameter('prefix');
answered Nov 15 '18 at 20:45
mytunymytuny
7616
7616
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Sep 19 '18 at 16:45
Chris StryczynskiChris Stryczynski
4,36153276
4,36153276
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%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
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