Laravel | Redirect Does Not Change URL - laravel

I am using an API from a payments-provider.
At some point, I show the user a page where he can authenticate himself (enter a code that the payment-provider will send him).
Note that on this page, I render a div element that the payment-provider has given me through an API request I made previously (I used Guzzle for this).
After the user enters the code and clicks submit, the payment-provider redirects him to one of my pages - let's say /redirect.
So, in my web.php I have a route:
Route::post('/redirect', 'OrdersController#redirect');
In my OrdersController I do this:
// do various stuff like updating the order status
return redirect("orders/$order->id");
There is another route in web.php:
Route::get('/orders/{order}', 'OrdersController#show');
And here is the show method:
return view('orders/show', [
'order' => $order,
'page_title' => 'Order Receipt',
]);
Two things go wrong:
The user gets shown the orders/show view but it seems like it is in an iframe:
The URL of the page does not change. It does not show myapp.test/orders/435 for example. Instead, it still shows the URL of the previous page (the one where the user authenticated himself) - something like myapp.test/authenticate let's say.
Any ideas why this is happening?
Update with screenshot:

Related

Clicking Reset password link redirects to login screen

here is my GET requests
http://localhost:8001/password/reset?token=11a1e66a78bd542b8021b31c8ba914a3ee8a556611b82fb187247d4c440a3b6f
here is my routes
Route::group(['middleware' => ['guest']], function () {
Route::post('api/v1/password/email', 'Auth\PasswordController#sendResetLinkEmail');
Route::get('api/v1/password/reset/{token}', 'Auth\PasswordController#getReset');
});
but when placing breakpoints, the flow does not reach getReset inside PasswordController.
It just redirects to the login screen http://localhost:8001/#/login
Im not sure what other info is needed.
EXTRA
Im not using Laravel's out of the box form etc. I have React as a front end, so I just need to get into the getReset function and will no the rest from there.
Your GET Request needs to be
http://localhost:8001/api/v1/password/reset/11a1e66a78bd542b8021b31c8ba914a3ee8a556611b82fb187247d4c440a3b6f
Because laravel can't find what route you are searching for.
If you want the GET Request unchanged then you can edit your route
Route::get('api/v1/password/reset/', 'Auth\PasswordController#getReset');
Just remove the {token}

Laravel Redirect as POST

Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });
POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.
You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.
For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.
return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])

codeigniter - Do I need a Controller for every URL?

I have a working project on Codeigniter 3. Now I have to build a FAQ page and I had this doubt: do I need a Controller for every URL?
It is, the FAQ page is a static page, but CodeIgniter generally routes URLs to Controllers, like domain/controller/method. But it seems a waste to build a Controller to only load the View.
No, it's not right way to make controller for every page. Just make one function which shows page by fetching data from database.
First of all make a table named pages in your database then save page_content, page_name, permalink for your different pages.
Now suppose your default controller is home, make a function in it with name page as below.
function pages( $permalink )
{
// get page data based on page_name passed in URL
$this->db->where( array( 'permalink' => $permalink ) );
$data['page'] = $this->db->get( 'pages' )->result();
// load view and pass page object to view
$this->load->view( 'view_file', $data );
}
Now same function will show different page content based on permalink passed in URL.
For example if URL is www.example.com/index.php/home/pages/faq then content of faq page will be shown.

php redirect to another page in laravel

I have a modal view (the one from bootstrap) in the front end.
Upon clicking the submit button the user will be going to a function in controller:
Route::post('post_question', array('uses' => 'QuestionController#postQuestion'));
And at the end of the postQuestion i want to redirect to another page.
I tried:
return Redirect::to('mywebsite/question/1');
return Response::make( '', 302 )->header( 'Location', 'mywebsite/question/1' );
return Redirect::route('question/'.$question_id)->with("question",Question::find($question_id));
header("Location: mywebsite/question/$question_id");
none seem to work though.
The thing is, i can see the request in XHR but just that the page is not redirected.
Is the modal somehow blocking the behavior?
You can redirect with an AJAX request. However, you will find that the results will not be quite what you expected.
On a redirect, Laravel will should set your response code header as a redirect response and then the content of the redirected page would be sent.
You could do one of two things depending on how you wanted to handle things.
Send a JSON response back to the submitted form with a meta data parameter and then use this meta data in your success function to set window.location.
Your Laravel controller responding to the post would look a bit like this:
public function postQuestion()
{
// DO stuff to set your $question
return [
'question' => $question,
'meta' => [
'redirect_url' => url('mywebsite/question/'.$question->id),
'status' => '400',
// Any other meta data you may want to send
],
];
}
Then assuming you are doing some jQuery AJAX call, change your success callback (I'm calling it questionSubmitSuccess here):
questionSubmitSuccess = function (data) {
// Anything you may want to do before redirecting the user
if (data.meta.redirect_url) {
// This redirects the page
window.location = data.meta.redirect_url;
}
}
Continue redirecting from your controller and then do something a bit more similar to rails turbo links and replace the entire page with Javascript:
You can do this a few ways: using [Modify the URL without reloading the page browser History API), or using jQuery.load to submit your form.
The browser history API might work a bit easier as it would still allow you to handle response errors, but it only works in more modern browsers.
jQuery.load would likely require rewriting a bit of your AJAX submitting code and is harder to handle things like errors (it will replace your page content no matter the status code from what I can tell), but it has better browser support.
IMO, the first approach is a bit more maningful as the API endpoint is usable by something other than this single implementation.
Also, there are fewer points of failure and error states to manage compared to trying to replace your entire DOM without a page reload.
U can put button inside form and when u submit that button pass data from page to controller and from controller call the another page with that data
like return View::make('users.index,compact('data'));

Controller redirect back to a POST form

I have a table showing a list of names, with an "edit" button and hidden id value at the side.
Clicking the "edit" button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.
When editing the details and submitting I'm using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a "Controller Method Not Found" error as there is no Get route set up.
Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:
public function postEditsave(){
...
if ($validator->fails())
{
return Redirect::to('admin/baserate/edit')
->withErrors($validator)
->withInput();
}else{
...
thanks
You can use Redirect::back()->withInput();
You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method
See: http://laravel.com/docs/5.0/responses
You don't need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters
You'd have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.
It'll look like this (notice the parameters):
public function getEdit($id)
{
return View::make(....);
}
public function postEdit($id)
{
...
return Redirect::back()->withErrors($validator)->withInput();
}
if "redirect with POST" is exist, then I don't know it. I recomend you to just use flash data
Redirect::to('user/login')->with('id', 'something');
You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

Resources