gpt4 book ai didi

laravel-blade - 在哪里添加参数到路由: verification.通知{语言}/email/verify

转载 作者:行者123 更新时间:2023-12-02 23:06:39 26 4
gpt4 key购买 nike

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

使用本地化后,我将 laravel 电子邮件验证添加到我的项目中。但现在我遇到了 Route:verification.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');
});
});

用户 Controller

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

最佳答案

简单地:覆盖中间件: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);
}
  • 修改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);
    }

    关于laravel-blade - 在哪里添加参数到路由: verification.通知{语言}/email/verify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59354860/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com