简体   繁体   English

在路由中使用可选参数

[英]Use optional parameter in route

I want to use same route in 2 scenarios,我想在两种情况下使用相同的路线,

  1. use route without passing parameter使用路由不传递参数
  2. use route with parameter使用带参数的路由

So, is it possible & how to do it?那么,是否有可能以及如何做到这一点?

You can optional parameters like this

Route::get('my-method/{param?}', function ($param= null) {
// your code here
});

In Web Php在 Web Php

Route::get('find-services/{service_name?}', 'commonController@find_services')->name('find-services');

and handle the parameters in commonController.php并处理commonController.php中的参数

public function find_services($service_name = null) {// Do something here}

Or Simple use the get method and check parameter in Controller或者简单使用get方法并检查Controller中的参数

public function find_services() {
    $input = Request::all();
    if (isset($input['service_name']) AND ! empty($_GET['service_name'])) {
        // Code
    }
}

I hope this one helps and For More Information Here => https://laravel.com/docs/7.x/routing#parameters-optional-parameters我希望这个有帮助,更多信息在这里=> https://laravel.com/docs/7.x/routing#parameters-optional-parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM