使用可选参数路由到控制器



我想创建一个采用所需ID和可选开始和结束日期("Ymd")的路线。如果省略日期,它们将回退到默认值。(说最近 30 天)并呼叫控制器....让我们说"path@index"

Route::get('/path/{id}/{start?}/{end?}', function($id, $start=null, $end=null)
{
    if(!$start)
    {
        //set start
    }
    if(!$end)
    {
        //set end
    }
    
    // What is the syntax that goes here to call 'path@index' with $id, $start, and $end?
});
无法

Route:::get闭包调用控制器。

用:

Route::get('/path/{id}/{start?}/{end?}', 'Controller@index');

并处理控制器函数中的参数:

public function index($id, $start = null, $end = null)
{
    if (!$start) {
        // set start
    }
        
    if (!$end) {
        // set end
    }
        
    // do other stuff
}

这有助于我简化可选的路由参数(来自 Laravel 文档):

有时,您可能需要指定路由参数,但使该路由参数的存在是可选的。您可以通过放置一个 ?在参数名称后标记。确保为路由的相应变量提供默认值:

Route::get('user/{name?}', function ($name = null) {
    return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
    return $name;
});

或者,如果您的路由中有控制器调用操作,则可以执行以下操作:

web.php
Route::get('user/{name?}', 'UsersController@index')->name('user.index');

userscontroller.php
public function index($name = 'John') {
  // Do something here
}

我希望这有助于有人像我一样简化可选参数!

Laravel 5.6 路由参数 - 可选参数

我会用条路径来处理它:

Route::get('/path/{id}/{start}/{end}, ...);
Route::get('/path/{id}/{start}, ...);
Route::get('/path/{id}, ...);

请注意顺序 - 您希望首先检查完整路径。

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

在此处查找更多详细信息(Laravel 7):https://laravel.com/docs/7.x/routing#parameters-optional-parameters

您可以从路由闭包调用控制器操作,如下所示:

Route::get('{slug}', function ($slug, Request $request) {
    $app = app();
    $locale = $app->getLocale();
    // search for an offer with the given slug
    $offer = AppOffer::whereTranslation('slug', $slug, $locale)->first();
    if($offer) {
        $controller = $app->make(AppHttpControllersOfferController::class);
        return $controller->callAction('show', [$offer, $campaign = NULL]);
    } else {
        // if no offer is found, search for a campaign with the given slug
        $campaign = AppCampaign::whereTranslation('slug', $slug, $locale)->first();
        if($campaign) {
            $controller = $app->make(AppHttpControllersCampaignController::class);
            return $controller->callAction('show', [$campaign]);
        }
    }
    throw new SymfonyComponentHttpKernelExceptionNotFoundHttpException;
});

我所做的是将可选参数设置为查询参数,如下所示:

示例网址: /getStuff/2019-08-27?type=0&color=red

路线: Route::get('/getStuff/{date}','StuffStuffController@getStuff');

控制器:

public function getStuff($date)
{
        // Optional parameters
        $type = Input::get("type");
        $color = Input::get("color");
}
无需

太多更改即可解决问题

Route::get('/path/{id}/{start?}/{end?}', function($id, $start=null, $end=null)
{
    if(empty($start))
    {
        $start = Carbon::now()->subDays(30)->format('Y-m-d');
    }
    if(empty($end))
    {
        $end = Carbon::now()->subDays(30)->format('Y-m-d');
    }
    return AppHttpControllersHomeController::Path($id,$start,$end);
});

然后

class HomeController extends Controller
{
public static function Path($id, $start, $end)
    {
        return view('view');
    }
}

现在的最佳方法是

use AppHttpControllersHomeController;
Route::get('/path/{id}/{start?}/{end?}', [HomeController::class, 'Path']);

然后

class HomeController extends Controller
{
    public function Path(Request $request)
    {
        if(empty($start))
        {
            $start = Carbon::now()->subDays(30)->format('Y-m-d');
        }
        if(empty($end))
        {
            $end = Carbon::now()->subDays(30)->format('Y-m-d');
        }
        //your code
        
        return view('view');
    }
}

最新更新