Laravel 语言切换器,将附加参数传递给路由 [英] Laravel language switcher, pass additional parameters to a route

查看:21
本文介绍了Laravel 语言切换器,将附加参数传递给路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的想不出一个优雅的解决方案来解决这个问题,所以这里是:

I really can't think of an elegant solution to fix this problem so here it is:

我遵循了本教程 https://www.youtube.com/watch?v=KqzGKg8IxE4 为我的网站创建本地化选项.然而,集成身份验证是一种癌症,现在我面临着一个更大的问题,即需要获取参数的页面的路由.

I followed this tutorial https://www.youtube.com/watch?v=KqzGKg8IxE4 to create a localization option for my website. However, integrating the auth was cancer and now I'm faced with an even greater issue regarding the routing for pages that require a get parameter.

这是我在 app.blade.php 中用于语言切换器的刀片代码:

Here is my blade code inside the app.blade.php for the language switcher:

                            <language-switcher
                                locale="{{ app()->getLocale() }}"
                                link-en="{{ route(Route::currentRouteName(), 'en') }}"
                                link-bg="{{ route(Route::currentRouteName(), 'bg') }}"
                            ></language-switcher>

这是我的一些路线

Route::redirect('/', '/en');

Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');


Route::group([
    "prefix" => '{language}',
    'where' => ['language' => '(en||bg)'],
], function () {

    //Auth routes
    Auth::routes();

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

    //Handles the faq routes
    Route::resource('faq', 'FaqController');

});


Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');

});

它适用于不需要任何获取参数的页面,例如:登录/注册/主页,但是如果我使用需要的东西,例如 faq.edit,它需要一个 {id} 参数 -语言切换器会抛出错误,因为我没有向它传递 {id} 参数.

It works well for pages that don't require any get parameters to work with ex: Login / Register / Home page, however if i use something that requires like, faq.edit, which takes an {id} parameters - the language switcher will throw an error because I haven't passed an {id} parameter to it.

我能想到的唯一解决方案是在子刀片视图中添加语言切换器,然后从那里传递所需的参数,但这意味着我必须将语言切换器添加到每个子视图中,而不仅仅是一次在父母处.

The only solution I can think of is adding the language-switcher inside the child blade view and from there I pass the required parameters, however that implies that I have to add the language-switcher to every child view instead of only once at the parent.

推荐答案

可以通过以下步骤实现:

You can achieve it with the following steps:

  • 实现一个 URL 生成器宏,它可以更轻松地生成除语言外必须相同的 URL
  • 获取当前路由的参数
  • 将所选语言合并到其中
<?php

namespace App\Providers;

use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        UrlGenerator::macro('toLanguage', function (string $language) {
            $currentRoute = app('router')->current();
            $newRouteParameters = array_merge(
                $currentRoute->parameters(), compact('language')
            );
            return $this->route($currentRoute->getName(), $newRouteParameters);
        });
    }
}

在你的 Blade 文件中

And in your Blade file

@inject('app', 'Illuminate\Contracts\Foundation\Application')
@inject('urlGenerator', 'Illuminate\Routing\UrlGenerator')

<language-switcher
    locale="{{ $app->getLocale() }}"
    link-en="{{ $urlGenerator->toLanguage('en') }}"
    link-bg="{{ $urlGenerator->toLanguage('bg') }}"
></language-switcher>

你会注意到我使用了契约注入而不是使用门面.更多信息请访问 https://laravel.com/docs/7.x/contracts#contracts-vs-facades

You'll notice I used contracts injection instead of using facades. More informations here https://laravel.com/docs/7.x/contracts#contracts-vs-facades

如果您不知道 Laravel 宏,请在此处查看更多信息 https://tighten.co/blog/the-magic-of-laravel-macros

And if you don't known Laravel macros, more informations here https://tighten.co/blog/the-magic-of-laravel-macros

这篇关于Laravel 语言切换器,将附加参数传递给路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆