Laravel - Path of page from which request was made - laravel-5

How can I determine the path of the page from which the request was made?
I have tried $request->path() or Request::path() but these two return the path on which request is going..

You may try this:
$request->header('referer');
Also URL::previous will give you that URL, for example:
\URL::previous();
Update: You may use this to send a user back to form on failed validation:
return redirect()->back(); // You may use ->withInput()->withErrors(...) as well

Related

(CI 4) How to redirect within Controller?

how can I refer to another within one controller? In CI3 (for example in the controller client.php) I solved this as follows:
redirect('/Clients', 'refresh');
but that no longer seems to work in CI4. (Msg: "route cannot be found while reverse-routing.")
I Also tried
redirect()->route('/Clients');
but the error is the same.
redirect()->to('/Clients');
redirects nowhere (no output, nothing)
For a better understanding: I want to use a controller (e.g. Clients/create to Clients/details)
What you should notice is that redirect() does not just set headers like it used to do in CI3. In CI4 it returns a RedirectResponse object with which you can ask your controller to do a redirection.
To do so, you need to return this RedirectResponse object inside your controller. Without the return statement, the redirection won't happen.
An other thing to notice is that redirect() can be called with some "options" :
Going to a named route
return redirect()->route('named_route');
or
return redirect('named_route');
To use this, you need to add a named routes in your app/Config/Routes.php file :
$routes->get('/', 'MyController::index', ['as' => 'named_route']);
Going to a specific URI
return redirect()->to('Clients');
It will redirect you to your base url with /Clients at the end.
Please check out the doc for further informations : https://codeigniter.com/user_guide/general/common_functions.html#redirect
#ViLar gives the correct answer, but it's important to note that when using auto routing the CI3 version redirect('home'); becomes return redirect()->to('home');
It's confusing to just say "Going to a specific URI" and omit that this means auto routed controllers.

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

How to correctly redirect after doing POST in Laravel?

I have multiple routes with comments and when I click reply I get redirected to a route where I can post a reply to a comment. How can I correctly store the route from where I came and then redirect back to it after posting the reply?
I considered passing the URL::previous as a param and storing it into a hidden input, but if the page gets refreshed by the user it gets empty. Another way might be store in the session, but then I don't know how to reliably expire it...
Redirect back with success message
return redirect()->back()->with('success', 'Data added successfully');
use return redirect()->back();
Use return Redirect::back() function for the previous URL.
You can use return Redirect::back(); or return Redirect::to(URL::previous() . "#whatever");
You can keep 2-3-4-5 URLs in a session and you don't need to expire it. You can just limit number of kept URLs. Also, please check my answer to a similar question here.
Store url in the session when user access page, when you have your reply button. You dont have to expire it, it will get automaticaly updated when user visit next page with reply button.
session(['last_url' => 'Request::fullUrl()']);
Also dont forget to use namespace
Use Request;
If you really want to discard value from session after user redirects, you can use this:
return redirect()->url(Session::pull('last_url'));
And namespace
Use Session;
Simply you can do return redirect()->back();
Do like this,
First store url in session
$request->session()->put('previous-url', '/user/demo');
Use like this
$previous_url = Session::get('previous-url');
return redirect()->to($previous_url);

How do we protect our post data processing method in CodeIgniter controller from the external form?

I'm working on a small project with CodeIgniter which handling some post data submitted from the admin form page.
I do transfer the post data to a method in my controller and send it to the database.
Its working all the time.
Im thinking, what if someone make an external form with the exact same inputs name and action attribute with mine in the admin page (I dont know how to figure the inputs name out but this is just my wonder), and try to post some data to the controller?
I try to use session but I wonder if there are any way to protect that kind of inject method?
you can try before your form_validator with
if( $_SERVER['HTTP_REFERER'] == base_url()){
//form validator
}
else{
$this->session->set_flashdata('warning', 'You try to enter from an external web without permission');
redirect(base_url(), 'refresh');
}
There are a couple of things you can do.
First, since this is an admin only page I assume you have some kind of login and user verification in place.
You can use session data to store the successful admin login.
//admin log in OK
$this->session->set_userdata('admin_logged_in', TRUE);
In the method that processes the form post, confirm the user is logged in
if($this->session->userdata('admin_logged_in') !== TRUE)
{
redirect('somewhere_else');
return; //here in case the redirect call doesn't work
}
Second, since you are 'posting' to this page confirm that is the method the server has received - it MUST be post. If you are using CI version => 3.0 the do this
if($this->input->method() !== 'post')
{
//somebody is trying to fool you
redirect('somewhere_else');
return; //here in case the redirect call doesn't work
}
If you are using an earlier version of CI (before 3.0.x) do this
if(strtolower($this->server('REQUEST_METHOD') !== 'post')
{
//somebody is trying to fool you
redirect('somewhere_else');
return; //here in case the redirect call doesn't work
}
You also might want to consider the case where the session info checks out but it was not a POST request. That is very suspicious to me and it might be wise to destroy the session before redirecting.

How do you change the CakePHP model validation redirect?

CakePHP seems to redirect an invalid form back to the controller/action the form was sent from. But in my case, the form comes from controller/action/value and I need to validation redirect to go there.
I've tried adding redirects in my controller in the appropriate place to no avail. Any ideas?
You totally can do this. Just manually check the validation from the controller like this:
if ($this->ModelName->validates(array('fieldList' => array('field1', 'field2')))) {
// valid - do save here and continue
} else {
// invalid - do redirect here
}
You can read more here:
http://book.cakephp.org/1.3/view/1182/Validating-Data-from-the-Controller

Resources