【问题标题】:Concatenated routes, can i do this in laravel 5.2?级联路由,我可以在 laravel 5.2 中做到这一点吗?
【发布时间】:2016-12-03 17:00:06
【问题描述】:

有一个包含三个过滤器(位置、类别、事件类型)的提供商列表,我需要在路由中连接过滤器以使 url 更友好,就像 ej.:mysite.com/filter/location/科尔多瓦/事件类型/除草/类别/旅行 这些过滤器不是必需的,所以我可以只过滤位置,或位置和类别,事件类型和类别等。现在我的问题是,¿我可以在 laravel 5.2 中这样做吗?

我之前这样做是这样的:

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

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

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

我需要这样的东西(它给我一个错误,我只放了一个或两个参数)

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

假设我按位置和类别搜索,那么 url 将是这样的:mysite.com/location/cordoba/event_type//caption/travel 这会生成一个错误的 url,这给了我错误,那我该怎么办这个?

【问题讨论】:

  • 除了这是否可破解之外,您可能需要考虑到虽然理论上(根据 RFC)在 URL 中使用双斜杠是可以接受的,但您永远不知道某些浏览器或链接处理器是什么就像谷歌一样。当它认为这是一个糟糕的 url 时,我会选择 laravel。
  • 感谢您的回答,现在对于 laravel 来说,这是一个错误的 url,所以我不能用双斜杠创建一个 url。

标签: php laravel routing routes


【解决方案1】:

每次缺少参数时,Laravel 都不知道如何找到您的路线,因此您不能真正依赖

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

你应该有类似的东西 Route::get('/filtro/location/{localidad}/event_type/{event_type}/caption/{caption}', [ '使用' => 'ProviderController@filter', 'as' => 'site_filter_path' ]);

所以在这种情况下你可以调用

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

为了确保不会缺少参数。

我喜欢做什么

过滤是很难放入 URL 的东西。每次你有一个新的过滤器,你都会得到一个新的 url,这一点都不好,因为它很快就会变得混乱。人们所做的,通常是只为过滤器设置一个端点,并使用 url 参数来配置过滤器。 Laravel 分页是一种不使用路由,而是使用 url 参数的过滤器。所以我会这样做

Route::get('/filtro', [
    'uses' => 'ProviderController@filter',
    '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&amp;tipo_evento=Santa Maria&amp;rubro=Todos,所以它会调用

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

【讨论】:

  • Localidad::newQyery(); 中有一个错字
  • 感谢您的回答,它对我帮助很大,但在我看来(前端做到了)我有一个带有链接的列表,所以我不能使用表单。将 ebay 过滤器想象成这样。
  • 你不需要使用表单,这只是一个例子。
  • 我知道,但是有我的问题,我需要配置 laravel 路由来连接这些参数而不会让我抛出错误。我认为是这样的:Route::get('/filtro/{event_type?}-en-{location?}/{caption?}', [ 'uses' =&gt; 'ProviderController@filter', 'as' =&gt; 'site_filter_path' ]); 但在这里我需要在 rute 中设置一个条件,当不来 event_type、标题或位置时
  • 做了一些编辑来澄清你的问题和我的答案。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2015-04-11
  • 1970-01-01
  • 2011-03-08
  • 2011-01-30
相关资源
最近更新 更多