如何在Laravel 5.3中使用URL?

时间:2017-01-30 18:48:41

标签: laravel laravel-5.3

我在web.php创建路线但我需要编辑此路线。

route.php

Route::group(['prefix' => 'flight'], function () {
        Route::get('/{fromCity}-{toCity}/{fromDate?}/{toDate?}/{adult?}-{child?}-{infant?}', ['as' => 'flight', 'uses' => 'Site\Flight\IndexController@index'])
            ->where([
                'fromCity' => '[A-Z]+',
                'toCity' => '[A-Z]+',
                'fromDate' => '\d{4}\-\d{2}\-\d{2}',
                'toDate' => '\d{4}\-\d{2}\-\d{2}',
                'adult' => '[0-9]+',
                'child' => '[0-9]+',
                'infant' => '[0-9]+'
            ]);
    });

仅针对此URL路由工作:

http://localhost:8000/flight/IKA-LON/2017-02-11/2017-02-16/1-0-0

但我需要使用这个`URL:

http://localhost:8000/flight/IKA-LON/2017-02-11/1-0-0

或者:

http://localhost:8000/flight/IKA-LON

如何修改这三个URL的工作路线?

3 个答案:

答案 0 :(得分:2)

您无法跳过可选参数。你可以只留一个空白。但中间人需要在那里。或者你可以留空,或者最后两个。 你无法跳过

我建议你使用默认值。例如:

http://localhost:8000/flight/IKA-LON/2017-02-11/2017-02-16/1-0-0 http://localhost:8000/flight/IKA-LON/2017-02-11/NOW/1-0-0 http://localhost:8000/flight/IKA-LON/NOW/NOW/1-0-0

您可以使用 NOW TODAY 。或者您认为最适合您的代码。

或使用不同的网址结构:

http://localhost:8000/flight/IKA-LON?from=2017-02-11&to=2017-02-16&adult=1&child=0&infant=0

它不够干净,但你别无选择。你应该使用第一种或第二种方法。

答案 1 :(得分:1)

由于您要求对我的评论进行解释,这里有一个简化的答案:

在web.php(或api.php)中

Route::get('flight', 'FlightController@index');

在FlightController中

function index(Request $request) {
    $url_path = str_replace ($request->url(), '', $request->fullUrl());
    $url_path_array = explode( '/' , $url_path);
    foreach ($url_path_array as $part) {
        // do something with the parts (based on regex or order)

    // flight regex: 
    $re1 = '/([A-Z]{3}-[A-Z]{3})/';
    // adult etc:
    $re2 = '/([0-1]{1}-[0-1]{1}-[0-1]{1})/';
    // from and to 
    $re3 = '/([0-9]{4}-[0-9]{2}-[0-9]{2})/';

    }
}

<强> REGEX

((?P<from>[A-Z]{3})-(?P<to>[A-Z]{3}))|((?P<adult>[0-1]{1})-(?P<child>[0-1]{1})-(?P<infant>[0-1]{1}))|(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})

regex demo

答案 2 :(得分:1)

就个人而言,我会用几条路线来实现这个目标:

Route::group(['prefix' => 'flight/{fromCity}-{toCity}'], function () {
    // Matches: /flight/IKA-LON
    Route::get('/', 'YourController@flights');

    // Matches: /flight/IKA-LON/2017-02-11/2017-02-16/1-0-0
    Route::get('{fromDate}/{toDate}/{adult}-{child}-{infant}', 'YourController@flightsFromTo');

    // Matches: /flight/IKA-LON/2017-02-11/1-0-0
    Route::get('{fromDate}/{adult}-{child}-{infant}', 'YourController@flightsFrom');
});

该论坛允许您在所有路线中匹配{fromCity}{toCity}

在您的控制器中,如果您要在每个路径中显示相同的内容并运行相同的功能,您可以在控制器中创建每个方法调用的方法:

class YourController extends Controller
{
    public function doFlights($fromCity, $toCity, $fromDate, $toDate, $adults, $children, $infants)
    {
        // Do your stuff
    }

    public function flights($fromCity, $toCity)
    {
        return $this->doFlights($fromCity, $toCity, date('Y-m-d'), date('Y-m-d'), 1, 0, 0);
    }

    public function flightsFromTo($fromCity, $toCity, $fromDate, $toDate, $adults, $children, $infants)
    {
        return $this->doFlights($fromCity, $toCity, $fromDate, $toDate, $adults, $children, $infants);
    }

    public function flightsFrom($fromCity, $toCity, $fromDate, $adults, $children, $infants)
    {
        return $this->doFlights($fromCity, $toCity, $fromDate, $fromDate, $adults, $children, $infants);
    }
}
相关问题