How can I let the web page automatically navigate based on time - laravel-5

I currently use laravel-5.8 and I want the welcome page can automatically jump to the thank you page when the time is after Oct 26th, 2019. That is, after Oct 26th, every time you open the localhost 8000, the web browser will show thank you page instead of welcome page.

You could just do a conditional redirect in the controller function of the route.
So you check if the current date is equal to, or greater than, the date you want. If so - redirect to the thank you route. Otherwise serve the welcome view.
Carbon::parse("Oct 27th, 2019"); // will return 2019-10-27 00:00:00.0.
So as soon as you pass the last second of Oct 26th you'll be equal or greater to this date.
You could do something like this (with better function and route names of course):
routes/web.php
Route::get('/', 'MyController#welcomeFunction')->name('welcome');
Route::get('/', 'MyController#thanksFunction')->name('thanks');
app/Http/Controllers/MyController.php
function welcomeFunction() {
$now = Carbon::now();
$targetDate = Carbon::parse("Oct 27th, 2019");
if ($now >= $targetDate ) {
return redirect(route('thanks'));
}
return view('welcome');
}
function thanksFunction() {
return view('thanks');
}

Related

Laravel facing issue in Redirecting

i am working on PTC script where i am facing problem.when i view third page or second page ads then after confirming ad it will automatically redirect back to ads page but i want to redirect it back to the current page(for example if i view second page ad then after confirm it has to redirect to second page)
Here's the code.
UserAdvert Controller.php
session()->flash('message', 'This Ads Has Been Successfully Viewed.');
Session::flash('type', 'success');
Session::flash('title', 'Earn Successful');
return redirect()->route('userCash.links');
}
public function cashLinkShow($id)
{
$advert= Advert::findOrFail($id);
return view('user.viewads.showads', compact('advert'));
}
Route.php
Route::get('user/cash/links', 'UserAdvertsController#cashLinks')->name('userCash.links');
Route::get('user/cash/link/show/{id}', 'UserAdvertsController#cashLinkShow')->name('userCashLinks.show');
Route::get('user/cash/link/confirm/{id}', 'UserAdvertsController#cashLinkConfirm')->name('userCashLink.confirm');
Route::get('user/cash/links?page=links', 'UserAdvertsController#cashLinkPage')->name('userCash.links.page');
Site is redirecting to this address after viewing ads
Route::get('user/cash/links', 'UserAdvertsController#cashLinks')->name('userCash.links');
but i want to redirect back to the current page from where ad is view i created route for this
Route::get('user/cash/links?page', 'UserAdvertsController#cashLinkPage')->name('userCash.links.page');
but i don't know how to get parameter of paginated page.. kindly help me to resolve this issue.I am beginner in laravel thanks
You should try this:
use Redirect;
return Redirect::to('/user/cash/links');

how to stay on the last view after reloading the page (fullcalendar)

I use fullcalendar in laravel 5.6.
My default view is the month view, and I want to stay on the last view after reloading the page.
example: if I switch to the week view, I want to be able to stay on this view week after refreshing the page and not return to the month view.
Thank you for helping me if you have a solution.
For V5:
var iv = localStorage.getItem("fcDefaultView") || 'timeGridWeek'
var id = localStorage.getItem("fcDefaultDate") || new Date
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: iv,
initialDate: id,
datesSet: function (dateInfo) {
localStorage.setItem("fcDefaultView", dateInfo.view.type);
localStorage.setItem("fcDefaultDate", dateInfo.startStr);
},
})
calendar.render()
It is really simple, just add the following lines to your .fullCalendar initialization:
viewRender: function (view, element) {
// when the view changes, we update our localStorage value with the new view name
localStorage.setItem("fcDefaultView", view.name);
},
defaultView: (localStorage.getItem("fcDefaultView") !== null ? localStorage.getItem("fcDefaultView") : "month"),
Rgds. Senad
use jquery cookie library to store current view using the viewRender event and then use the follwing method to switch to it,
$('#calendar').fullCalendar( 'changeView', 'basicDay' );
here is the reference for viewRender event which is triggered while you switch among views.
https://fullcalendar.io/docs/viewRender
For V4:
viewSkeletonRender: function (view, element) {
// when the view changes, we update our localStorage value with the new view name
localStorage.setItem("fcDefaultView", view.view.type);
},
defaultView: (localStorage.getItem("fcDefaultView") !== null ? localStorage.getItem("fcDefaultView") : "month"),
I had problems with localStorage. For me it only works with localhost, but not on a remote server.
Instead I use ajax and php to store the variable in $_SESSION. Note that the startStr variable returns the first box date, which is the previous month if the first day of the month is not a Sunday. This is corrected for in the php code, by adding 7 days.
Remember to include a session_start(); at the top of your main file.
PHP & Javascript in main file:
var calendarEl = document.getElementById('calendar');
<?php
if(isset($_SESSION['startdate'])) echo "var id = '".$_SESSION['startdate']."';";
else echo "var id = new Date;"
?>
var calendar = new FullCalendar.Calendar(calendarEl, {
initialDate: id,
datesSet: function (dateInfo) {
$.post("./setdate.php", {startdate : dateInfo.startStr}); // posts current view date for storage in $_SESSION
},
.....
PHP code (setdate.php):
<?php
session_start();
$date = strtotime(substr($_POST['startdate'], 0, 10));
$date = strtotime("+7 day", $date); // the datestring relates to the first box, which is usually the previous month, so add a week to get to correct month
$_SESSION['startdate'] = date("Y-m-d", $date);
?>
My example only stores the month, not the view, but the code can easily be extended to include that.

Laravel 4.0 Filter and Ajax to load a bootstrap modal pop up

I'm doing a website based laravel 4.2 and Bootstrap. I want a bootstrap modal pop up dialog to load on certain routes provided that the user is not already logged in.
So far i have accomplsihed it to load for the entire application upon loading which is not i want but am in the right direction.
This is my ajax call
script type="text/javascript"> $(document).ready(function() {
$.get("status", function(data, status){
data.status == false ? $('#modal-form').modal({backdrop: 'static'}) : $('#modal-form').modal('hide');
});
});
Status refers to a URL defined in this route
Route::get('/status', 'LoginController#getLoginStatus');
and the method is defined here
public function getLoginStatus()
{
return Response::json(array( 'status' => Sentry::check()));
}
From that, the modal dialog loads on each route across the entire application. I would want to limit the dialog to load on certain routes provided the user is not logged in.
Thing Laravel filter would do the trick for me but i have failed to do so.
Something like
Route::filter('status', function()
{
});
and then the route be like:
Route::get('profile', array('as' => 'submit-profile','before' => 'status','uses' => 'ProfileController#getProfile'));
Thanks guys hope you can give me some advice.
You could use the
Route::currentRouteName()
to check what route your on and then do your preferred action,
like calling the ajax get request for the modal.

Laravel: strange behavior when I try to serve ajax and non-ajax request on same route (Caching)

I registered a GET route for laravel.dev/test. The corresponding controller for the route would distinguish whether the request is ajax or not.
When I type laravel.dev/test on the browser, Laravel detect that it's not an ajax request and uses return View::make() to generate the page. Then Backbone.js code on the page make an ajax request to laravel.dev/test and Laravel uses return Response::json to return a JSON.
It's all fine until when I try to navigate away from the page and then use the browser button to navigate back to laravel.dev/test that it print out the json as the response, which is not what I expect since I'm not making an ajax request.
Definitely a caching issue. Just to try and get some results, add this to your controller (ajax and non-ajax) to force-disable caching:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
And see if chrome still fetches from the cache on the back button.
This is not a laravel or backbone, but a chrome issue! Check this out too.
The solution that worked for me is to put
return Response::json($this->data)->header("Vary", "Accept");
Good luck!
This is in Laravel 5.1, but the principle should work for all previous versions as well.
The way I handled it was with two routes pointing to the same method, but with one ending in a .json extension:
get('items', ['as' => 'items', 'uses' => 'ItemsController#index']);
get('items.json', ['as' => 'items', 'uses' => 'ItemsController#index']);
Then inside my index() method:
$data = []; // your collection
if ($this->request->ajax()) {
return response()->json($data); // replace with actual JSON data
}
return view('items.index', compact('data'));
This allows for a dedicated URL for JSON responses, uses the same method and data, and never interferes with my back button.

ZF2 Session not lasting on redirect

My site uses multiple languages and my users can click on flags to set their desired language. When that flag is clicked, a Session should store that information and then i want my controller to redirect the user to another page. This i do with the following code:
<?php
public function setLangAction () {
$oLanguageCookie = new Container('language');
$oLanguageCookie->lang = $this->params ('langvar');
$this->redirect()->toRoute('loadpage', array('page' => 'home'));
}
?>
However, when i print_r($_SESSION) in the indexAction (the action where loadpage routes to), $_SESSION is empty.
Can somebody help me?
Depending where you param comes from you should execute
$this->params()->fromQuery('langvar');
$this->params()->fromPost('langvar');
unless it is a route parameter then you can use either:
$this->params()->fromRoute('langvar');
$this->params('langvar');

Resources