2016-12-03 65 views
1

有三個過濾器(位置,類別,事件類型)的提供者列表,我需要在路由中連接過濾器以使url更友好,這就像ej .: mysite.com/filter/location/cordoba /事件類型/除草/類別/旅行 這些過濾器不是必需的,所以我可以只過濾位置,或位置和類別,事件類型和類別等。現在我的問題是,我可以做laravel 5.2嗎?連接路線,我可以在laravel 5.2中做到這一點嗎?

我之前做到這一點是這樣的:

Route::get('/filtro/localidad/{localidad?}', [ 
     'uses' => '[email protected]', 
     'as' => 'site_filter_location_path' 
    ]); 

    Route::get('/filtro/tipo_evento/{event_type?}', [ 
     'uses' => '[email protected]', 
     'as' => 'site_filter_event_type_path' 
    ]); 

    Route::get('/filtro/rubro/{caption?}', [ 
     'uses' => '[email protected]', 
     'as' => 'site_filter_caption_path' 
    ]); 

我需要的是這樣的(它送我,我把只有一個或兩個參數的誤差)

Route::get('/filtro/location/{localidad?}/event_type/{event_type?}/caption/{caption?}', [ 
    'uses' => '[email protected]', 
    'as' => 'site_filter_path' 
]); 

比方說,我搜索通過位置和類別,然後該網址將是這樣的:mysite.com/location/cordoba/event_type//caption/travel這會產生一個錯誤的網址,這給了我錯誤,那麼我該怎麼做呢?

+1

除了這幸福您可能需要考慮的是,雖然在理論上(根據RFC)在URL中使用雙斜槓是可以接受的,但您永遠不會知道某些瀏覽器或鏈接處理程序(如Google)如何處理它。我認爲這是一個糟糕的網址時與laravel。 –

+0

感謝您的回答,現在laravel這是一個糟糕的網址,所以我不能用雙斜槓做網址。 –

回答

3

Laravel不知道如何找到你的路線每次你有一個缺少的參數的時間,所以你不能真正依靠

Route::get('/filtro/location/{localidad?}/event_type/{event_type?}/caption/{caption?}', [ 
    'uses' => '[email protected]', 
    'as' => 'site_filter_path' 
]); 

你應該有類似 路線::得到('/ filtro /位置/ {localidad}/event_type/{event_type}/caption/{caption}',[ '使用'=>'ProviderController @ filter', 'as'=>'site_filter_path' ]);因此,在這種情況下

你可以叫

/filtro/location/Santito/event_type/Fiesta/caption/false 

可以肯定就不會有缺少的參數。

我喜歡做

過濾的東西很難把URL中。每當你有一個新的過濾器,你會得到一個新的網址,這是不好的,因爲它非常快速地變得混亂。人們通常的做法是僅爲過濾器設置一個端點,並使用url參數來配置過濾器。 Laravel分頁是一種不使用路由的過濾器,而是url參數。所以我會做

Route::get('/filtro', [ 
    'uses' => '[email protected]', 
    'as' => 'site_filter' 
]); 

而且這樣

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

class ProviderController extends Controller 
{ 
    private function buildFilter($request, $query) 
    { 
     if ($localidad = $request->get('localidad')) { 
      $query->where('localidad', $localidad); 
     } 

     if ($tipoEvento = $request->get('tipo_evento')) { 
      $query->where('tipo_evento', $tipoEvento); 
     } 

     ... 

     return $query; 
    } 

    public function filter(Request $request) 
    { 
     $query = Localidad::newQuery(); 

     $query = $this->buildFilter($request, $query); 

     $query->where('filter something else'); 

     return view('home', ['data' => $query->get()]); 
    } 
} 

控制器然後你只需要使用method="GET"過濾

<form action="/filtro" method="GET"> 
    ... 
</form> 

做一個表單,您的應用程序應該會收到類似

請求
/filtro?localidad=Santa Maria&tipo_evento=Fiesta 

如果你需要打你自己的路,做內部過濾器,你可以做這樣的事情:

Route::get('/filter', function() { 
    $items = [ 
     'localidad' => 'Buenos Aires', 
     'tipo_evento' => 'Santa Maria', 
     'rubro' => 'Todos', 
    ]; 

    $items = collect($items)->map(function($item, $key) { 
     return "$key=$item"; 
    }); 

    $filter = implode('&', $items->toArray()); 

    return redirect()->to('/filter?'$filter); 
}); 

在這個例子中$filterlocalidad=Buenos Aires&tipo_evento=Santa Maria&rubro=Todos,所以它會調用

/filter?localidad=Buenos Aires&tipo_evento=Santa Maria&rubro=Todos 
+0

你有一個與Localidad :: newQyery(); –

+0

感謝您的回答,它對我有很大的幫助,但在我看來(前端是這麼做的)我有一個鏈接列表,所以我不能使用表單。把這個ebay過濾器做成這樣的東西。 –

+0

你不需要使用表單,它只是一個例子。 –