Laravel 7重定向问题

时间:2020-05-11 06:19:14

标签: laravel

在表单成功通过验证/存储并通过电子邮件发送后,我试图将消息重定向到视图(通知)。

一切正常,但在发送电子邮件后无法重定向。 表单验证后尝试重定向,并且可以正常工作。但是,如果我呼叫store()并尝试在save()之后重定向,它将不会重定向。

尽管我的store()函数可能出了点问题,所以我创建了一个空函数test()并尝试重定向(如果这样)

public function test($request){
    Session::flash('message', 'alert-danger');
    return Redirect::to('/notifications');
}

public function test(){
    Session::flash('message', 'alert-danger');
    return Redirect::to('/notifications');
}

它仍然不起作用。

但如果从validator()重定向,则可以正常工作。

我不知道怎么了。请帮忙。

    <?php

namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Mail\RegistrationMail;
use Illuminate\Support\Facades\Mail;
use App\Register;
use Illuminate\Support\Facades\URL;
use Session;
use Illuminate\Support\Facades\Redirect;


class RegisterController extends Controller
{


  protected function store(Request $request){
    $length = 16;
    $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code = substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    $user = new Register();
    $user->fname = request('fname');
    $user->mname = request('mname');
    $user->lname = request('lname');
    $user->email = request('email');
    $user->kathum_tui = $code;
    $user->created = \Carbon::now();
    $vfname = request('fname');
    $vlname = request('lname');
    $vemail = request('email');
    $url =  URL::temporarySignedRoute('verify', now()->addMinutes(1500), ['fname' => $vfname ,'lname'=> $vlname , 'email'=> $vemail]);
    $user->save();
    Mail::to($user['email'])->send(new RegistrationMail($user,$url));
    Session::flash('message', 'alert-danger');
    return Redirect::to('/notifications');
  }


  protected function validator(Request $request)
  {
    $validator = Validator::make($request->all(),[
      'email' => ['required', 'string', 'email', 'max:255', 'unique:registers'],
    ]);

    if ($validator->fails()) {
       $request->validate( [
        'fname' => ['required', 'string', 'max:255'],
        'mname' => ['nullable','string', 'max:255'],
        'lname' => ['required', 'string', 'max:255'],
        ],
      [
        'fname.required' => 'First Name Cannot be empty!',
        'lname.required' => 'Last Name Cannot be empty!',
        'email.unique' => 'You have not completed the Registration. Please check your email to complete the Registration.',
      ]
    );


  }else{
    $request->validate( [
      'fname' => ['required', 'string', 'max:255'],
      'mname' => ['nullable','string', 'max:255'],
      'lname' => ['required', 'string', 'max:255'],
      'email' => ['required', 'string', 'email', 'max:255', 'unique:registers'],
    ],
    [
      'fname.required' => 'First Name Cannot be empty!',
      'lname.required' => 'Last Name Cannot be empty!',
      'email.unique' => 'An account already Exist with this email. Forgot Password',
    ]
    );
    $this->store($request);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您应该在验证器函数中返回存储方法调用,如下所示:

return $this->store($request);