简体   繁体   English

laravel 本地化设置默认 locale 参数替换其他参数

[英]laravel localization set the default locale parameter replacing other parameters

I am new to laravel.我是laravel的新手。 when I set the default locale from localization.php and I click on any of my data list edit buttons it replaces the id variable to locale value like (en/ ur/ sd).当我从 localization.php 设置默认语言环境并单击我的任何数据列表编辑按钮时,它会将 id 变量替换为语言环境值,如 (en/ur/sd)。

then I remove the default locale parameter from Localization.php然后我从 Localization.php 中删除默认的语言环境参数

ill get Missing parameter: about生病了缺少参数:关于

web.php网页.php

Route::group([
    'prefix' => '{locale}', 
    'where' => ['locale' => '[a-zA-Z]{2}'], 
    'middleware' => 'Localization'
    ], function() {


        Route::get('/', function () {
            return redirect(route('home'));
        });
        
Route::resource('/about', AboutController::class)->middleware('auth');
});

kernel.php内核文件

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

        // Permission
        'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
        'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
        'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
        'Localization' => \App\Http\Middleware\Localization::class,
    ];

Localization.php本地化.php

class Localization
{
    /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
    public function handle(Request $request, Closure $next)
    {        

        URL::defaults(['locale' => $request->segment(1)]);
        app()->setLocale($request->segment(1));
        return $next($request);
        
        
    }
}

AboutController.php edit function AboutController.php 编辑功能

public function edit($id)
    {
       
        $singleRow = About::find($id); 
        $allRows = About::all();
        return view('about', compact('allRows','singleRow'));
    }

app.php应用程序.php

'locale' => 'en',
'fallback_locale' => 'en',

    'available_locales' => [
         'en',
         'sd',
         'ur',
      ],

about.blade.php Edit section when I click here for edit data about.blade.php 编辑部分,当我点击这里编辑数据时

if I try without app()->getLocale() and the locale parameter is assigned from Localization.php I ll replace the id as selected locale language like en, sd, ur whatever is given in URL like http://127.0.0.1:8000/en/about/1/edit如果我尝试不使用 app()->getLocale() 并且 locale 参数是从 Localization.php 分配的,我会将 id 替换为选定的区域设置语言,如 en、sd、ur 在 URL 中给出的任何内容,如http://127.0.0.1 :8000/en/about/1/edit

1st try with default locale parameter mentioned above第一次尝试使用上面提到的默认语言环境参数

<a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

second try without locale parameter mentioned above第二次尝试没有上面提到的语言环境参数

<a href="{{ route('about.edit', ['locale'=> app()->getLocale(), 'about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

in this time I have set the default locale from Localization.php mentioned above and removed the locale parameter from my tag这次我从上面提到的 Localization.php 中设置了默认语言环境,并从我的标签中删除了语言环境参数

<a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

I get this in my browser when I clicked on edit button当我点击编辑按钮时,我在浏览器中看到了这个

changed return to dd更改返回到 dd

public function edit($id)
    {
       
        dd($id);
        $singleRow = About::find($id); 
        $allRows = About::all();
        return view('about', compact('allRows','singleRow'));

    }

图像在这里

in this time I commented on the locale parameter from Localization.php and added the locale parameter in my tag这次我评论了 Localization.php 中的 locale 参数并在我的标签中添加了 locale 参数

<a href="{{ route('about.edit', ['locale'=> app()->getLocale(), 'about' => $about->id]) }}" class="btn btn-primary">Edit</a>

在此处输入图片说明

Localization.php本地化.php

class Localization
    {
        /**
        * Handle an incoming request.
        *
        * @param  \Illuminate\Http\Request  $request
        * @param  \Closure  $next
        * @return mixed
        */
        public function handle(Request $request, Closure $next)
        {        
    
            URL::defaults(['locale' => $request->segment(1)]);
            app()->setLocale($request->segment(1));
            return $next($request);
            
            
        }
    }

language switcher where i was getting the error.我收到错误的语言切换器。 in master.blade.php在 master.blade.php 中

@foreach (config('app.available_locales') as $locale)
            <li class="dropdown messages-menu">
                <a class="nav-link"
                href="{{ route(\Illuminate\Support\Facades\Route::currentRouteName(), ['locale'=>$locale,0]) }}"
                    @if (app()->getLocale() == $locale) style="font-weight: bold; text-decoration: underline" @endif>{{ strtoupper($locale) }}</a>
            </li>
        @endforeach

here in master.blade.php i set $locale with ['locale'=> $locale, 0] i don't know second parameter is why required but its working fine在 master.blade.php 中,我将 $locale 设置为 ['locale'=> $locale, 0] 我不知道为什么需要第二个参数,但它工作正常

and i have changed the controller edit function as我已经将控制器编辑功能更改为

public function edit($ignore='', $id)
    {
       
        $singleRow = About::find($id); 
        $allRows = About::all();
        return view('about', compact('allRows','singleRow'));
    }

here i am getting the id when i click on the edit在这里,当我点击编辑时,我得到了 id

<a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM