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.










share|improve this question






















  • 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

















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.










share|improve this question






















  • 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















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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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



















active

oldest

votes











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',
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%2f53423286%2flaravel-redirect-route-to-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





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.




draft saved


draft discarded














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





















































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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo