2

嗨有下一条路线

Route::get('/{param1}',
        'Http\Controllers\SomeController@getCollection');
Route::get('/{param_for_middleware}/{param1}',
        'Http\Controllers\SomeController@getCollection');

而且,在我的控制器上,我有

// PSR7 code
use Illuminate\Routing\Controller;
use Psr\Http\Message\ServerRequestInterface;

class SomeController extends Controller
    public function getCollection(ServerRequestInterface $request)
    {
        // here, I need access to param1
        dd($request);
    }
}

使用原始 Laravel 路由器我们可以通过$this->route()->parameters()['param1]. 但是,当我们在 Laravel 上使用 PSR7 时呢?

4

1 回答 1

1

你可以,还在使用旧的方法吗?

public function getCollection(ServerRequestInterface $request, $param1)
{
    // here is your param1!
    dd($param1);
}

编辑

如果您需要多个参数并且需要确保它们的位置,您可以这样做:

public function getCollection(
    ServerRequestInterface $request,
    \Illuminate\Http\Request $illuminateRequest
)
{
    // here is your param1 & param_for_middleware!
    dd($illuminateRequest->route('param1') . ' & ' .
       $illuminateRequest->route('param_for_middleware'));
}
于 2018-01-31T06:13:53.903 回答