Laravel redirect route to API
up vote
0
down vote
favorite
I have the following route in my routes/web.php file of Laravel 5.7.
Route::match(['get', 'post', 'put', 'delete'], '{request}', function ($request) {
return redirect('api/'.$request);
})->where('request', '^(?!api).*');
This route rule checks for any routes that does not start with "api", prepend "api" to the route and redirect it.
This works well for GET requests but does not work for POST, PUT or DELETE requests. I get an HttpException when I attempt to POST something. If I define individual POST, PUT or DELETE route in the routes/web.php file, I get the same result. The request seems to fail immediately hence the route is never executed or fails during execution.
Route::post('some/feature/{feature_id}/{sub_feature_id}', 'API/SomeFeature@store');
Here is the exception I get.
{
"exception": "Symfony\Component\HttpKernel\Exception\HttpException",
/* ... */
}
We have an old system that uses the following routing structure.
feature/{feature_id}/{sub_feature_id}
The new system I am working on has the following routing structure.
api/v1/feature/{feature_id}/sub_feature/{sub_feature_id}
What I need to do is redirect all requests not starting with api to their counterparts in the new system.
If I define the routes of the old system in the routes/api.php routes file, they automatically get prefixed with "api" which is not my desired behaviour because our old system does not have "api" prefixed.
Are there any way I can get this to work for POST, PUT and DELETE requests?
My best guess is those verbs are disabled in routes/web.php for API based requests and only executes when submitted by a form on the same domain. Alternatively it may be CSRF blocking the request, perhaps something that I am missing.
Any advise would be highly appreciated.
laravel-routing php-7.1 laravel-5.7 laravel-request laravel-resource
add a comment |
up vote
0
down vote
favorite
I have the following route in my routes/web.php file of Laravel 5.7.
Route::match(['get', 'post', 'put', 'delete'], '{request}', function ($request) {
return redirect('api/'.$request);
})->where('request', '^(?!api).*');
This route rule checks for any routes that does not start with "api", prepend "api" to the route and redirect it.
This works well for GET requests but does not work for POST, PUT or DELETE requests. I get an HttpException when I attempt to POST something. If I define individual POST, PUT or DELETE route in the routes/web.php file, I get the same result. The request seems to fail immediately hence the route is never executed or fails during execution.
Route::post('some/feature/{feature_id}/{sub_feature_id}', 'API/SomeFeature@store');
Here is the exception I get.
{
"exception": "Symfony\Component\HttpKernel\Exception\HttpException",
/* ... */
}
We have an old system that uses the following routing structure.
feature/{feature_id}/{sub_feature_id}
The new system I am working on has the following routing structure.
api/v1/feature/{feature_id}/sub_feature/{sub_feature_id}
What I need to do is redirect all requests not starting with api to their counterparts in the new system.
If I define the routes of the old system in the routes/api.php routes file, they automatically get prefixed with "api" which is not my desired behaviour because our old system does not have "api" prefixed.
Are there any way I can get this to work for POST, PUT and DELETE requests?
My best guess is those verbs are disabled in routes/web.php for API based requests and only executes when submitted by a form on the same domain. Alternatively it may be CSRF blocking the request, perhaps something that I am missing.
Any advise would be highly appreciated.
laravel-routing php-7.1 laravel-5.7 laravel-request laravel-resource
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following route in my routes/web.php file of Laravel 5.7.
Route::match(['get', 'post', 'put', 'delete'], '{request}', function ($request) {
return redirect('api/'.$request);
})->where('request', '^(?!api).*');
This route rule checks for any routes that does not start with "api", prepend "api" to the route and redirect it.
This works well for GET requests but does not work for POST, PUT or DELETE requests. I get an HttpException when I attempt to POST something. If I define individual POST, PUT or DELETE route in the routes/web.php file, I get the same result. The request seems to fail immediately hence the route is never executed or fails during execution.
Route::post('some/feature/{feature_id}/{sub_feature_id}', 'API/SomeFeature@store');
Here is the exception I get.
{
"exception": "Symfony\Component\HttpKernel\Exception\HttpException",
/* ... */
}
We have an old system that uses the following routing structure.
feature/{feature_id}/{sub_feature_id}
The new system I am working on has the following routing structure.
api/v1/feature/{feature_id}/sub_feature/{sub_feature_id}
What I need to do is redirect all requests not starting with api to their counterparts in the new system.
If I define the routes of the old system in the routes/api.php routes file, they automatically get prefixed with "api" which is not my desired behaviour because our old system does not have "api" prefixed.
Are there any way I can get this to work for POST, PUT and DELETE requests?
My best guess is those verbs are disabled in routes/web.php for API based requests and only executes when submitted by a form on the same domain. Alternatively it may be CSRF blocking the request, perhaps something that I am missing.
Any advise would be highly appreciated.
laravel-routing php-7.1 laravel-5.7 laravel-request laravel-resource
I have the following route in my routes/web.php file of Laravel 5.7.
Route::match(['get', 'post', 'put', 'delete'], '{request}', function ($request) {
return redirect('api/'.$request);
})->where('request', '^(?!api).*');
This route rule checks for any routes that does not start with "api", prepend "api" to the route and redirect it.
This works well for GET requests but does not work for POST, PUT or DELETE requests. I get an HttpException when I attempt to POST something. If I define individual POST, PUT or DELETE route in the routes/web.php file, I get the same result. The request seems to fail immediately hence the route is never executed or fails during execution.
Route::post('some/feature/{feature_id}/{sub_feature_id}', 'API/SomeFeature@store');
Here is the exception I get.
{
"exception": "Symfony\Component\HttpKernel\Exception\HttpException",
/* ... */
}
We have an old system that uses the following routing structure.
feature/{feature_id}/{sub_feature_id}
The new system I am working on has the following routing structure.
api/v1/feature/{feature_id}/sub_feature/{sub_feature_id}
What I need to do is redirect all requests not starting with api to their counterparts in the new system.
If I define the routes of the old system in the routes/api.php routes file, they automatically get prefixed with "api" which is not my desired behaviour because our old system does not have "api" prefixed.
Are there any way I can get this to work for POST, PUT and DELETE requests?
My best guess is those verbs are disabled in routes/web.php for API based requests and only executes when submitted by a form on the same domain. Alternatively it may be CSRF blocking the request, perhaps something that I am missing.
Any advise would be highly appreciated.
laravel-routing php-7.1 laravel-5.7 laravel-request laravel-resource
laravel-routing php-7.1 laravel-5.7 laravel-request laravel-resource
asked Nov 22 at 3:04
Kobus Beets
1
1
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04
add a comment |
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:
RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:
RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53423286%2flaravel-redirect-route-to-api%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
Any ideas how I can achieve this using .htaccess? I have tried a few example to no avail. My last .htaccess attempt was the following:
RewriteCond %{REQUEST_URI} ^((?!api).*)
RewriteRule ^ api%1 [L,R=301]
– Kobus Beets
Nov 22 at 22:04