【问题标题】:Where to add parameter to Route: verification.notice {language}/email/verify在哪里添加参数到 Route:verification.notice {language}/email/verify
【发布时间】:2020-04-08 19:29:54
【问题描述】:

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

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

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

web.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'));
    }
}

----编辑----

SetLanguage.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);
    }
}

【问题讨论】:

  • 显示您的 SetLanguage 中间件代码。
  • @DilipHirapara 我添加了我的 SetLanguage.php 文件
  • @400028588 你是怎么解决这个问题的?

标签: laravel-blade laravel-6 laravel-authentication laravel-localization


【解决方案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);
    }

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 正如我所说,我检查了路径的所有视图,但找不到它
    【解决方案3】:

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

    $this->redirectTo = ($user->locale == 'pl') ? 'https://example.net/pl/dziekujemy' : 'https://example.net/en/thank-you';
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      相关资源
      最近更新 更多