Laravel 4 change route based on current URL - php

I have a login form spread across my website on different pages for different situations that are all used in the same POST controller. In the POST controller I always redirect->back() so it goes to the previous page the visitor was on. However, now I need it setup differently. I need it to know which page they came from and based on that previous page know which page to redirect the user to.
So basically I need a Route::getCurrentRoute()->getPath() for the previous page. So it can be something like this:
Route::getCurrentRoute()->getPath() == "some/url" then go to some/url/2
If I actually use getCurrentRoute() it just shows my POST route and not the route the visitor is coming from.

Related

Laravel custom urls saved in database

I want to give custom urls to members of my site to have their pages under my site url like www.mysite.com/{theirurls}.
In database in user table i have a field userurl that keeps the url i give them.
In route i have written the code
Route::get('/{fpage?}', 'AController#fofpage');
In controller in fofpage function i take the variable and look for the value in database. If it belongs to someone redirects to this. If it isnt goes back to / (root).
The problem is that all the other urls like login and others doesnt work.
How can i solve this problem?
Thanks in advance
Put this route at the end of the web routes file to make it work:
Route::get('/{fpage?}', 'AController#fofpage');
You'll also want to validate user URLs and not allow users to enter values like login, register etc.

Harsh redirect back() Laravel5.2

I am having a small issue with my Laravel 5.2 application. The problem is with a page made to adjust database entries.
When data has been adjusted and a button is pressed to update the database, it works great. Problem is, I would like to redirect back() to the previous page, but this page has been set up through data from other pages.
So when I redirect back, it error's and says that certain parameters are not defined. Is there a option, that I can implement a very static redirect to the previous page and all the data that has been loaded in the page?
Like the go back function that web browsers have?
Solution
I added the following code in the Controller function where the page is made to adjust the database entry:
Session::flash('url',Request::server('HTTP_REFERER'));
And as a redirect I have this:
return Redirect::to(Session::get('url'));

Keeping track of more than one level of page referrers

The scenario (all happening within the administration area/backend):
From the listing page, the user clicks a link to view an article (on the backend).
From the article view page, the user clicks a link to edit that article.
In the article edit page, form is submitted to the current uri.
If validation succeeds or user cancels, user is redirected to the article view page.
From the article view page, the user click a 'back' link to return to the listing page.
List <--> View <--> Edit
Right now, I'm only able to track referring url from a previous page. In the edit form, I'm using a hidden field to maintain referral to the view page, lest it be changed during failed form POST submission to itself and user remains in the edit page.
Problem is that when the user returns to the view page from edit, the 'back' link to the listing page is now linked to the edit page.
FYI,
The listing page url is dynamic as the user should return to the listing on the same page and sort order (stored in query strings); therefore a fixed url is out of the question.
In the past, I've tried using sessions (e.g. SESSION['view_to_list_ref'] SESSION['edit_to_view_ref']), but it messed up with multiple tabs.
I could transition between view/edit via ajax, but I'm hoping to keep the app simple and ajaxless at this point of time.
I'm using PHP + Kohana 3.2 Framework
The only solution I can think of is to have the list page url encoded and appended to the 'view article' link via query string. This way, the location of the listing page is preserved even while in the edit page; as the referring url back to view page would also contain the listing page url in the query string. However I don't really like the idea of 'dirtying' the url with long parameter values (encoded or not).
I'm really hoping there is a more elegant solution to this problem of generally tracking multiple levels of page referrals; not just specifically to solving the scenario I've mentioned.
EDIT: Oh and the solution should be able to support multiple tabs performing the same scenario.
You could track the pages by using a unique identifying code in a PHP session, a temporary variable, and using a temporary database table that tracks page loads by these temporary values.
The database structure might be:
+-------------+-------------------+---------------------+
| Unique ID | Page Referral | Time of page load |
+-------------+-------------------+---------------------+
Tracking time of page load would allow you to selectively wipe loads older than X minutes, and keep the table relatively small.
Anyway, this would allow you to keep as many levels as you'd like, and if you wanted to add an auto incrementing counter field, or your own counter field, you could even keep a simple to use number system that tracks page loads, though I believe the time of page load would suffice for that scenario.

PHP, Returning to a calling Page after saving a record in a Edit page

Lets say I have a Page with a List (list.php).
I click on a row on that list to Edit that record. I go to a edit.php Page.
I have 3 buttons on that edit.php page. Save, Apply, Cancel
Save button - Saves the Record and returns to the (list.php) Page
Apply button - Saves Record but stays on the same page (edit.php)
Cancel button - No save, just return to the (list.php) Page
But now image if I can access for edit that item on a different page. How do I return to that calling page?
Do I add a parameter(code) to the URL? something like a Page Origination Code?
Do I save the previous page URL in a session? (bad, they can right click open another page and that would be saved to session url)
Am just curious to how others return to a previous page after a SAVE.
you can the server variable $_SERVER['HTTP_referrer'].
They are other ways also you can store in session the current page and use is processing page.
Adding a parameter to the URL is the only reliable though quote ugly way.
That's why such an in-place editions nowadays often being implemented using AJAX, and this very site is a perfect example.
However, there are different cases.
Login page is imperfect example for example, as you always have a form instead of just a link, and thus you can always store the current page in a hidden form field.
Another approach is possible if you are using some sort of front controller, and all requests actually being directed to the single index.php file which runs appropriate script based on the URI.
in this latter case you will need no more than mere a redirect to the current page.

Action called from many other places, how to handle a "Back" button?

I have an action (view for example) in a controller that is called from multiple other actions in other controllers. How is the best way to create a "Back" button that will take me back to the page that got me here?
I've used named parameters like "back_controller" and "back_action" and that works fairly well but they get awkward when the page has a form that gets submitted. I have to be sure to pass those parameters as hidden fields or in the form url and then look for them after the form has been processed.
Is there some kind of stack or other solution that anyone else has come up with that handles this situation better? I see this problem in a lot of my projects and I've yet to come up with a good solution.
I don't completely understand your question, but this may be helpful:
If you need to redirect to the referer page you can use:
$this->redirect($this->referer());
http://book.cakephp.org/view/425/redirect
I don't believe in back buttons. That is a feature that the browser does quite well and you would be better off having buttons always taking you to specific destinations rather than back.
If you must have a back button, you could create a history stack in the session. When a page loads you just push that page on the history (you will want to make sure you don't push the same page on the stack multiple times). You could create URL like /back who's sole job is to redirect the user to the last page they were on.
$_SERVER['HTTP_REFERER']

Categories