2016-12-03 61 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

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