Laravel (5)-通过可选参数路由到控制器

我想创建一个路由,该路由采用必需的 ID 和可选的开始和结束日期(‘ Ymd’)。如果省略了日期,它们将回到默认值。(比如说最后30天)然后调用一个控制器... . let Say‘ 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?
});

任何帮助都将不胜感激。我确信有一个简单的答案,但我在任何地方都找不到任何东西。

先谢谢你的帮助!

143220 次浏览

无法从 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
}

我会用 路径来处理它:

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


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


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

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

您可以像下面这样从路由闭包调用控制器操作:

Route::get('{slug}', function ($slug, Request $request) {


$app = app();
$locale = $app->getLocale();


// search for an offer with the given slug
$offer = \App\Offer::whereTranslation('slug', $slug, $locale)->first();
if($offer) {
$controller = $app->make(\App\Http\Controllers\OfferController::class);
return $controller->callAction('show', [$offer, $campaign = NULL]);
} else {
// if no offer is found, search for a campaign with the given slug
$campaign = \App\Campaign::whereTranslation('slug', $slug, $locale)->first();
if($campaign) {
$controller = $app->make(\App\Http\Controllers\CampaignController::class);
return $controller->callAction('show', [$campaign]);
}
}


throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


});

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

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

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

总监:

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

这帮助我简化了可选的路由参数(来自 Laravel Docs) :

有时您可能需要指定一个路由参数,但是可以选择是否存在该路由参数。你可透过以下方式办理:?在参数名称后面加上标记。确保给路由的相应变量一个默认值:

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('user/{name?}', function ($name = null) {
return $name;
});

更多详情请点击这里(Laravel7) : https://laravel.com/docs/7.x/routing#parameters-optional-parameters

解决你的问题,不需要做太多改变

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 App\Http\Controllers\HomeController::Path($id,$start,$end);
});

然后

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

现在最好的办法是

use App\Http\Controllers\HomeController;
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');
}
}