簡體   English   中英

Laravel mcamara/laravel-localization package 通過查看

[英]Laravel mcamara/laravel-localization package pass to view

Laravel 版本 7.6.2

我正在嘗試使用 mcamara/laravel-localization package。

https://github.com/mcamara/laravel-localization

我按照他們在 github 頁面中給出的說明進行了一些個性化設置,允許我使用另一個字段而不是使用 id 來訪問。

這些是我的路線:

use Illuminate\Support\Facades\Route;


Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
    ],function () {

    Route::get('/', 'HomeController@index')->name('home');
    Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@index')->name('vehicle');
    Route::get(LaravelLocalization::transRoute('routes.vehicle/{vehicle_url}'), 'VehicleController@show')->name('vehicle.show');

});

車輛表結構:

public function up()
{
    Schema::create('vehicles', function (Blueprint $table) {
        $table->id();
        $table->string('vehicle_url')->unique();
        $table->string('name_en');
        $table->string('name_de');
        $table->text('descr_en');
        $table->text('descr_de');
        $table->timestamps();
    });
}

Vehicle Slug 表結構:

public function up()
{
    Schema::create('vehicle_slugs', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('vehicle_id');
        $table->string('locale',2);
        $table->string('slug')->unique();
        $table->timestamps();
    });
}

此車輛 Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\VehicleSlugs;


class Vehicle extends Model implements \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable
{

    public function getRouteKeyName()
    {

        return 'vehicle_url';

    }


    public function slugs()
    {
        return $this->hasMany(VehicleSlugs::class);
    }

    public function getLocalizedRouteKey($locale)
    {

        return $this->slugs->where('locale', '=', $locale)->first()->slug;
    }

    public function resolveRouteBinding($slug, $field = NULL)
    {
        return static::whereHas('slugs', function ($q) use ($slug) {
            $q->where('slug', '=', $slug);
        })->first() ?? abort('404');
    }
}

這是 Controller:

namespace App\Http\Controllers;

use App\Vehicle;
use Illuminate\Http\Request;

class VehicleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('vehicles');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Vehicle  $vehicle
     * @return \Illuminate\Http\Response
     */
    public function show(Vehicle $vehicle_url)
    {
        return view('vehicle', compact('vehicle_url'));
    }

這是vehicle.blade.php:

<!DOCTYPE html>
<html lang="{{app()->getLocale()}}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Testing mcamara</title>
</head>
<body>

<a href="{{route('home')}}">Home</a>
<a href="{{route('vehicle')}}">Vehicle</a>
@forelse(App\Vehicle::all() as $vehicle)
<a href="{{route('vehicle.show', $vehicle->vehicle_url)}}">{{$vehicle->name_en}}</a> <!-- For Example Bikes/Motocycles/Cars/.. -->
@empty
<a href="#">No Link</a>
@endforelse

</body>
</html>

這是用於翻譯的數組(/resource/lang/de/routes.php)。

return [
    "vehicles"   => "fahrzeuge",
    "vehicle/{vehicle_url}"  =>  "fahrzeug/{vehicle_url}",
]; //The English file is the same with vehicles and vehicle instead of fahrzeuge and farhzeug.

好吧,如果我在地址欄上鍵入app.loc/de/fahrzeuge/fahrrader它可以完美運行,我可以通過正確的翻譯到達自行車頁面,但無法翻譯app.loc/de/fahrzeuge/{slug} slug使用視圖中的鏈接。

我當然想念一些東西。 有人可以幫忙嗎?

我遇到了同樣的問題,我一直在尋找並且我給了這篇文章,它與這里的解決方案有關,但我使用了以下包Astrotomicmcamara

顯然你有一個錯誤的聲明你的翻譯路線,你必須 go 沒有參數

Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@show')->name('vehicle.show');

路由文件用相應的語言解決

/resource/lang/de/routes.php

這是我在之前的數據包中使用的解決方案。

    public function state(State $state)
    {
        $state = State::whereTranslation('slug', $state->slug)->firstOrFail();
        if ($state->translate()->where('slug', $state->slug)->first()->locale != app()->getLocale()) 
        {
        return redirect()->route('state.show', $state->translate()->slug);
        }

        return view('front.states.state', compact('_label', 'state'));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM