2

SEO の目的で、e コマース ストアのルートを平坦化しようとしています。

次のルートを作成したいと思います。

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry']);
Route::get('/{category}, ['uses' => 'Store\ProductController@browseCategory']')

countryandはcategory動的でなければなりません。

次のようなことが可能かどうか知りたいですか?そして達成するための最良の方法。

// Route 1
Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
    ->where('country', ProductCountry::select('slug')->get());

// Route 2
Route::get('/{category}', ['uses' => 'Store\ProductController@browseCategory'])
    ->where('category', ProductCategory::select('slug')->get());

ルート例:

/great-britain should be routed via Route 1
/china         should be routed via Route 1

/widgets       should fail route 1, but be routed via Route 2 because 
               widgets are not in the product_country table but are in 
               the product_category table

可能な国でルートをハードコーディングできることを知っています:

Route::get('/{country}', ['uses' => 'Store\ProductController@browse'])
    ->where('country', 'great-britain|china|japan|south-africa');

しかし、これは不器用で面倒です。データベースから国のリストを取得したいと思います。

4

3 に答える 3

3

私はこの方法でそれを行います モデルが少ないため国のモデルを選択します+それをキャッシュする必要があります: list('name') を国名の列に変更します

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
->where('country', implode('|',ProductCountry::select('slug')->lists('name')));

すべての国名を選択し、このような配列として返すことです

 ('usa','england','thailand') 

'|' で implode を使用します。接着剤としてこれを返します:

usa|england|thailand

したがって、最終的なルートは次のようになります。

Route::get('/{country}', ['uses' => 'Store\ProductController@browseCountry'])
->where('country', 'usa|england|thailand');
于 2013-12-17T12:41:40.793 に答える
0

これを実現するには、ルート フィルターが必要です。

次のコードをファイルfilters.phpまたはroute.phpファイルに配置します

Route::filter('country', function()
{

    $country = Country::where('slug', Route::input('country'))->first();
    if(  ! $country) {
        dd("We do not support this country");
            // Redirect::route('home');
    }

});

そして最後にあなたのルート:

Route::get('country/{country}', array('before' => 'country', 'uses' => 'Store\ProductController@browseCountry'));
于 2013-12-17T11:44:21.913 に答える