Laravel not redirects page after sending mail - laravel

I am sending a mail and after mail is send I want to redirect user to a specific url
$mail = Mail::send('test.mail', ['a' => 'a'], function (Message $message) {
$message->to(['hod#vu.edu.pk', 'student#vu.edu.pk',]);
$message->from('stms#vu.edu.pk');
$message->subject('Test Mail');
});
// dd($mail);
return response()->redirectToRoute('allocate_supervisor')
->with('message', 'Supervisor Assigned and sent to HoD for approval.');
I have tried to return redirect()->route(), url(), and redirect('allocate_supervisor') but each time same issue.
dd($mail); works fine and shows output but on redirect it shows blank page.
Also request status code is 200.
Also tried
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
before return still no output

There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:
return redirect('/allocate_supervisor');
make sure 'allocate_supervisor' route must be present in your web.php file.
if you have named routes, you may use route() method.
return redirect()->route('/allocate_supervisor');

I was calling storePhase function from store function and returning response from storePhase function which was being overridden in store function and I was receiving blank page...
I moved this response code to store function and it worked fine.

Related

How to use SignedRoute on example of unique register URL

I created unique url registration, when someone uses link, he will be able to register to my app.
The only problem is, my signed route is invalid and gives 0 on hasValudSignature, where might be the problem?
My register function in RegisterController:
public function register(Request $request) {
abort_unless($request->hasValidSignature(), 403, 'That link has expired or is no longer valid!');
//they can register now
}
My URL generator:
$url = URL::temporarySignedRoute(
'register',
now()->addDays(7)
);
After using my user index view it is displayed on page so I can copy link for new user. However when I try to register using this link, at the end I've got information about wrong Validation (error I made by myself to prevent from registering unauthorized people)
Is my $url written wrongly?
Edit
For some reason after typing in console php artisan route:list
I've got error that
ReflectionException::("Class "UserController" does not exist")
How might this be possible?
Edit
After remakeing controller, I'm back to the original problem with hasValidSignature

Laravel flash message and redirection not working properly

I'm using laravel 5.8 and "laracasts/flash": "^3.0"
Within my application all redirections and flash messages are working well except for this very specific piece of code.
/* Controller */
public function show( Test $test) {
$test->checkPermission();
...
}
/* Model */
public function checkPermission()
{
flash()->warning('You can not have access to this.');
return redirect( route('home' ) )->send(); //Notice the send()
}
If i'm using this code with the ->send()(that I never used before) I'm well redirected to the homepage but without flash message.
If I remove the ->send(), I have got the flash message but I'm not redirected.
I also tried to remove the flash() and using redirect()->with(). Then the session is containing the message and I'm redirected. But I want to use flash() or atleast undertand why it's not working for this specific use-case.
The controller should return the redirect, not the check permission. Try to return what check permission is returning in the controller.
Try adding the web middleware on your routes. or ensure the web middleware group contains the StartSession middleware.
via https://laracasts.com/discuss/channels/laravel/auth-session-killed-after-redirect-laravel-52/replies/124991

Why can't this method access the session?

I have a session containing some data that I access throughout several methods in my controller with no trouble. But one method in particular has problems.
I have made a little booking system. On completion of a booking, I call a function to send an e-mail to myself and the customer:
public function complete(Request $request)
{
$details = Session::get('details');
// send emails: call sendConfirmationEmail(to address, using this view)
$this->sendConfirmationEmail(env('EMAIL'), 'emails.ourconfirmation');
$this->sendConfirmationEmail($details->booker_email, 'emails.bookerconfirmation');
return view('booking/complete');
}
private function sendConfirmationEmail($to,$view)
{
$from = env('OFFICE_EMAIL');
$to_address = $to;
$details = Session::get('details');
$instance = Session::get('instance');
Mail::queue($view, compact(['details','instance']), function($message) use ($to){
$message->from(env('OFFICE_EMAIL'))
->to($to)
->subject('Thanks for booking');
});
}
First I got an error accessing a non-object in my e-mail view. So to test it I just set the sendConfirmationEmail function to return $instance - blank page. Then I tested it by commenting out the function call in complete() and returning $instance there. No problem, there's a nice shiny session full of data. Then I tried passing $instance from complete() to sendConfirmationEmail() and returning it: again, blank page. Why can't sendConfirmationEmail 'see' my session?!
To my knowledge, Sessions are for using across HTTP requests, not across functions in PHP. You are trying the use Sessions as global variables.
You can simply capture details and instance session data in the complete function, and then pass them along as parameters for the sendConfirmationEmail() function.

Laravel 5 and Socialite - New Redirect After Login

Another newb question here, but hopefully someone can shed some light:
I am using Socialite with Laravel 5, and I want to be able to redirect the user to a page on the site after they have logged in. The problem is that using
return redirect('any-path-I-put-here');
simply redirects back to 'social-site/login?code=afkjadfkjdslkfjdlkfj...' (where 'social-site' is whatever site is being used i.e. facebook, twitter, google, etc.)
So, what appears to me to be happening is that the redirect() function in the Socialite/Contracts/Provider interface is overriding any redirect that I attempt after the fact.
Just for clarification, my routes are set up properly. I have tried every version of 'redirect' you can imagine ('to', 'back', 'intended', Redirect::, etc.), and the method is being called from my Auth Controller (though I have tried it elsewhere as well).
The question is, how do I override that redirect() once I am done storing and logging in the user with socialite? Any help is appreciated! Thank you in advance.
The code that contains the redirect in question is:
public function socialRedirect( $route, $status, $greeting, $user )
{
$this->auth->login( $user, true );
if( $status == 'new_user' ) {
// This is a new member. Make sure they see the welcome modal on redirect
\Session::flash( 'new_registration', true );
return redirect()->to( $route );// This is just the most recent attempt. It originated with return redirect($route);, and has been attempted every other way you can imagine as well (as mentioned above). Hardcoding (i.e., 'home') returns the exact same result. The socialite redirect always overrides anything that is put here.
}
else {
return redirect()->to( $route )->with( [ 'greeting' => $greeting ] );
}
}
... The SocialAuth class that runs before this, however, is about 500 lines long, as it has to determine if the user exists, register new users if necessary, show forms for different scenarios, etc. Meanwhile, here is the function that sends the information through from the Social Auth class:
private function socialLogin( $socialUser, $goto, $provider, $status, $controller )
{
if( is_null( $goto ) ) {
$goto = 'backlot/' . $socialUser->profile->custom_url;
}
if( $status == 'new_user' ) {
return $controller->socialRedirect($goto, $status, null, $socialUser);
}
else {
// This is an existing member. Show them the welcome back status message.
$message = 'You have successfully logged in with your ' .
ucfirst( $provider ) . ' credentials.';
$greeting =
flash()->success( 'Welcome back, ' . $socialUser->username . '. ' . $message );
return $controller->socialRedirect($goto, $status, $greeting, $socialUser);
}
}
I managed to workaround this problem, but I am unsure if this is the best way to fix it. Similar to what is stated in question, I got authenticated callback from the social media, but I was unable to redirect current response to another url.
Based on the callback request params, I was able to create and authenticate the user within my Laravel app. It worked good so far but the problems occured after this step when I tried to do a return redirect()->route('dashboard');. I tried all the flavours of redirect() helper and Redirect facade but nothing helped.
The blank page just stared at my face for over 2 days, before I checked this question. The behaviour was very similar. I got redirect from social-media to my app but could not further redirect in the same response cycle.
At this moment (when the callback was recieved by the app and user was authenticated), if I refreshed the page manually (F5), I got redirected to the intended page. My interpretation is similar to what's stated in this question earlier. The redirect from social-media callback was dominating the redirects I was triggering in my controller (May be redirect within Laravel app got suppressed because the redirect from social-media was still not complete). It's just my interpretation. Experts can throw more light if they think otherwise or have a better explaination.
To fix this I issued a raw http redirect using header("Location /dashboard"); and applied auth middleware to this route. This way I could mock the refresh functionality ,redirect to dashboard (or intended url) and check for authentication in my DashboardController.
Once again, this is not a perfect solution and I am investigating the actual root of the problem, but this might help you to move ahead if you are facing similar problem.
I believe you are overthinking this. Using Socialite is pretty straight forward:
Set up config/services.php. For facebook I have this:
'facebook' => [
'client_id' => 'your_fb_id',
'client_secret' => 'your_fb_secret',
'redirect' => '>ABSOLUTE< url to redirect after login', //like: 'http://stuff'
],
Then set up two routes, one for login and one for callback (after login).
In the login controller method:
return \Socialize::with('facebook')->redirect();
Then in the callback function
$fb_user = \Socialize::with('facebook')->user();
// check if user exists, create it and whatnot
//dd($fb_user);
return redirect()->route('some.route');
It should be pretty much similar for all other providers.
We are using the Socialite login in our UserController as a trait. We simply overrode the AuthenticatesSocialiteLogin::loginSuccess() in our controller.
use Broco\SocialiteLogin\Auth\AuthenticatesSocialiteLogin;
class UserController extends BaseController
{
use AuthenticatesSocialiteLogin;
public function loginSuccess($user)
{
return redirect()->intended(url('/#login-success'));
}
....

sess_create(); not working in Codeigniter 3.0 after sess_destroy();

Is it possible to keep flash data after session destroy in codeigniter.i saw several answers on stackoverflow but none of them works.
Below is my logout method.
public function logout() {
$this->session->sess_destroy();
$this->session->sess_create();
$this->session->set_flashdata('success', 'You have been logged out successfully.');
redirect('welcome');
}
Getting error message
Fatal error: Call to undefined method CI_Session::sess_create()
I m using codeigniter 3.0.
Do nothing just remove or comment the bottom line and try:-
$this->session->sess_destroy();
// $this->session->sess_create();
Also set in config.php file
$config['sess_match_useragent'] = FALSE;
and read this link
You can set the cookie instead of a session
$this->load->helper('cookie'); // Call the helper
$this->input->cookie('logoutstatus', TRUE); //set the cookie
In the page where you want to display the message, check for that cookie
$this->load->helper('cookie'); // Call the helper again in that view page
$checkStatus = get_cookie('logoutstatus');
if($checkStatus){ //if its available
//display thge message and then delete the cookie
delete_cookie("logoutstatus");
}
NOTE: It would be a lot better if you just include the cookie helper in your autoload.php configuration.

Resources