Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

James_Moore's avatar

Redirect to a view from api

After creating a model on the Controller in my API namespace, how do i redirect the user to another page. Currently, under Network, response, its just returning the html code to the view. So its hitting the route and returning the view, but how do i actually redirect and view the VIEW lol return redirect(url('/cms/requisitioners/' . '1' . '/edit')); the ones hardcoded for current testing purposes

0 likes
2 replies
martinbean's avatar
Level 80

@James_Moore A RESTful API shouldn’t return a redirect or a “view”; it should return an appropriate status code and ideally a representation of the newly-created resource.

If you’re creating resources, you should send a 201 Created status, and also include a Location HTTP header that is the URL of where the user can find the newly-created resource. This header’s value is where you want to redirect to.

If you’re using Axios to make AJAX requests, then that would look something like this:

axios.post(url, data).then(response => {
    window.location.href = response.headers.location;
});
1 like

Please or to participate in this conversation.