reusing common methods + redirect to current page |
[eluser]allibubba[/eluser]
let me see if i can explain myself here... i have a restrict user function that checks if a user has admin rights, if they don't, i grab the page they were trying to access and store in flash data, then bounce them to a login view, and after they pass login with admin rights i redirect back to the page they were trying to access (stored in flash data). this works great and is not the problem. what i am looking for are some ideas on how to continually store a current page, and have the ability to redirect users to the page they were previously on. example would be my add to cart function: Code: function add(){ after i add an item i want to be able to bounce back to the page they were on. how do you guys handle situations like this? do you use a redirect model and load it up for each controller where a redirect might be used? or should i be looking more in the direction of library?
[eluser]thinkigniter[/eluser]
Try Javascript History object http://www.w3schools.com/htmldom/met_his_back.asp
[eluser]allibubba[/eluser]
ummmm... and if JS is disabled? essentially what i'm looking for is a more streamlined way of grabbing the current page and storing. my method so far has been to load up my user model for whichever controller i'm in and run my function which stores current page in flash data... thinking i could do something more in a global state, where controller functions store the current page in a session variable, one that is constantly updated and available globally by just getting it from the session variable.
[eluser]Mirage[/eluser]
[quote author="allibubba" date="1216184717"]ummmm... and if JS is disabled? essentially what i'm looking for is a more streamlined way of grabbing the current page and storing.[/quote] I use a post controller constructor hook to access the session object and save the current url in a flashvar of the session. For your login example I make sure the flashdata survies failed logins. I use the PHP header() function to send a location redirect when necessary. Another way is to set an extra cookie containing the last url. I've recently download the Firebug extension FireCookie and it's absolutely amazing to see how many separate cookies almost every site sets that I frequent. And just as a comment - I wouldn't worry so much anymore about javascript being turned off. That's 90's stuff. It becomes increasingly difficult to imagine how anyone could survive a day on the web with javascript turned off. After all this is the Age of AJAX and that simply doesn't work without JS and neiter do Google Analytics and similar services. Imagine a CMS without Javascript (at some level at least). I just don't particularly like using javascript as a solution for this particular type of problem. Cheers, -m
[eluser]allibubba[/eluser]
thanks Mirage, i'll look a bit more into hooks, still learning the CI framework. just checked and my current solution does not survive a failed login... but when using flash data i need to continually keep it alive eg: keep_flashdata(); which means i have to have that in each function of my login/out check user process, which does seem like the wrong way to do things. i kinda like the idea of setting and updating a cookie, but will look further into using the header() function. just that updating a cookie means i have to load a model and run a cookie update function for every view change, so maybe see if i can work something out with hooks. trying to follow 'DRY' (don't repeat yourself) principals as much as possible. as for JS, yeah i completely agree (didn't want to start a debate on how many people do/don't use js), but for this round of exploration i want to do everything through CI, once i have a solid grasp of CI i'll be rebuilding with a robust JS front-end with mootools, but will also degrade to native CI functions if JS is disabled.
[eluser]Mirage[/eluser]
[quote author="allibubba" date="1216250532"]just checked and my current solution does not survive a failed login... but when using flash data i need to continually keep it alive eg: keep_flashdata(); which means i have to have that in each function of my login/out check user process, which does seem like the wrong way to do things.[/quote] Hmm, sounds like you need to refactor your controller methods. The flash data only needs to be kept if the login fails. That test needs to happen only once one would think. Of course you also always have the option not to use flashdata at all, but storing the 'login-target' in a fixed session variable and clear it when login succeeds. Sort of the other way around I suppose... Cheers, -m
[eluser]allibubba[/eluser]
just to follow up on this. rather than try to write a redirect model and use it for all my redirect needs, i've simply used php's header function to redirect from my add to cart function, and have continued to evolve my user model to not user flashdata, but store a redirect page in a session variable. think i was just making this more difficult than it needed to be thanks for the pointers mirage, they got me to rethink my approach.
[eluser]beemr[/eluser]
How about Code: function lastURI() I think it just requires the url helper
[eluser]Bramme[/eluser]
I'm not sure if referrer always works... Sometimes it gets blocked. I know, (or at least I think I know) because I've been pondering about this some time back now. If you ever get it sorted out properly allibubba, please post back!
[eluser]allibubba[/eluser]
what i ended up with for my add to cart functionality is this: Code: header('location:'.$_SERVER['HTTP_REFERER']); this works ok since you are on a product page, adding to cart just sends you back to where you were. though i think for production purposes i would facilitate the add to cart function via ajax. as for my user redirect, which does use flash data, and seems to finally be stable: im my user_model; Code: function restrictUser($role){ Code: $this->User_model->restrictUser('admin'); in my login function i needed to keep the flash data alive: Code: $this->session->keep_flashdata('requested_page'); as for the referrer, i haven't noticed any problems. working on creating a user library now, when i make some solid progress i'll post back. hope this helps somebody out |
Welcome Guest, Not a member yet? Register Sign In |