5

[Route: verify.notice] [URI: {language}/email/verify] 缺少必需参数

使用本地化后,我将 laravel 电子邮件验证添加到我的项目中。但是现在我遇到了 Route: verify.notice 缺少参数的问题。我知道我需要将 app()->getLocale() 参数添加/传递给路由,但找不到在哪里

我尝试搜索项目中的所有路由和 URL,还检查了 VerificationController.php 和 verify.blade.php。但是我没有找到缺少参数的路线。另外,我在网上找不到有同样问题的其他人。

网页.php

Route::group([
    'prefix' => '{language}',
    'where' => ['{language}' => '[a-Za-Z]{2}'],
    'middleware' => 'SetLanguage',
],
    function () {

        Route::get('/', function () {
            return view('welcome');
        })->name('Welcome');

        Auth::routes(['verify' => true]);

        Route::get('/home', 'HomeController@index')->name('home');

        Route::namespace('User')->group(function () {
            Route::get('/profile', 'UserController@editProfile')->name('profile');
            Route::put('profile', 'UserController@updateProfile');
        });

        Route::namespace('Admin')->group(function () {
            Route::get('/dashboard', 'AdminController@index')->name('dashboard');
        });
    });

用户控制器

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('verified');
    }

    public function editProfile()
    {
        $user = User::where('id', Auth()->user()->id)->first();

        return view('user.profile', compact('user'));
    }
}

- - 编辑 - -

设置语言.php

namespace App\Http\Middleware;

use App;
use Closure;

class SetLanguage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        App::setLocale($request->language);

        return $next($request);
    }
}
4

3 回答 3

1

简单:覆盖中间件:EnsureEmailIsVerified

  1. 创建一个具有相同名称的新中间件并插入:app()->getLocale();
public function handle($request, Closure $next, $redirectToRoute = null)
{
  if (! $request->user() ||
     ($request->user() instanceof MustVerifyEmail &&
       ! $request->user()->hasVerifiedEmail())) {
      return $request->expectsJson()
          ? abort(403, 'Your email address is not verified.')
          : Redirect::route($redirectToRoute ?: 'verification.notice', app()->getLocale());
  }
  
  return $next($request);
}
  1. 修改App\Http\Kernel.php并替换:
\Illuminate\Auth\Middleware\EnsureEmailIsVerified

经过

\App\Http\Middleware\EnsureEmailIsVerified::class

最后, verification.verify 路由也可能有问题

使用这样的新通知类覆盖此路由:注意:URL::temporarySignedRoute可以传递语言等参数

<?php

namespace App\Notifications;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Config;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class VerifyEmail extends Notification
{
    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return (new MailMessage)
            ->subject(Lang::get('Activer mon compte client'))
            ->line(Lang::get('Veuillez cliquer sur le bouton ci-dessous pour activer votre compte client.'))
            ->action(Lang::get('Activer mon compte client'), $verificationUrl)
            ->line(Lang::get('Si vous n\'avez pas demandé la création d\'un compte client '.config('app.name').', ignorez simplement cet e-mail.'));
    }

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'language' => app()->getLocale(),
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

并将声明添加到用户模型中:

// OVERRIDE
    /**
     * Send email verification.
     * @call function
     */
    public function sendEmailVerificationNotification() {
        $this->notify(new VerifyEmail);
    }
于 2021-07-06T11:07:41.477 回答
0

解决方法是保持原样,但仅将 url 更改为“验证后”页面。您可以通过在创建新用户时存储用户的语言环境来做到这一点,然后在 VerificationController 中的验证方法中执行以下操作:

$this->redirectTo = ($user->locale == 'pl') ? 'https://example.net/pl/dziekujemy' : 'https://example.net/en/thank-you';
于 2020-04-16T09:17:47.047 回答
0

我以前也遇到过和你一样的问题。

正如您所说,您没有在视图中的某些路径之前添加 {language}/ 检查所有视图中的路径并将其更改为如下所示:

href="{{ route('somePath', app()->getLocale() ) }}"

确保更改所有页面以在您的视图中包含带有语言前缀的正确路径。

于 2019-12-16T11:03:50.647 回答