laravel - 使用 Route 对象从控制器方法内部获取路由参数是否被认为是一种不好的做法?[拉拉维尔]
问题描述
我有多个路由可以解析为单个控制器方法。我Route
用来获取路由参数,因为 Laravel 按照定义的顺序将参数传递给方法。尽管该Route
方法有效,但它增加了我们单元测试的复杂性。有什么建议么?
<?php
// routes/api.php
$router->get('/test/{another_example_param}', ['uses' => 'ExampleController@show']);
$router->get('/test/{example_param}/thing/{another_example_param}', ['uses' => 'ExampleController@show']);
$router->get('/testing/{example_param}', ['uses' => 'ExampleController@show']);
<?php
// ExampleController.php
use Illuminate\Routing\Controller
use Illuminate\Routing\Route;
class ExampleController extends Controller {
// Explicitly defining route parameters
public function show(string $example_param, string $another_example_param) {
echo "example param: $example_param";
echo "another_example param: $another_example_param";
}
// Extracting route params from Route approach
public function showAlternate(Route $route) {
$example_param = $route->example_param;
$another_example_param = $route->another_example_param;
echo "example param: $example_param";
echo "another_example param: $another_example_param";
}
}
解决方案
推荐阅读
- ios - 安装了 FB 应用程序的 SwiftUI 自定义 Facebook 登录
- docker - 在 cmd 或 powershell 中从 Windows 10 主机运行 Docker 容器内的脚本
- sql - N 个项目的 SQL DB 结构,其中 M 个参数随时间变化
- python - 熊猫:计算不同字母在单元格中出现的频率
- r - R:为网络分析准备数据
- python - 将字符串列转换为 pandas DataFrame 中的布尔指标列
- blazor - 无法在源选项卡的 chrome 可调试窗口中找到 Blazor 文件
- python - 使用 DST 小时查找 timedelta 差异(Python)
- python - Python 和 AT 命令
- angular - 如何为角材料创建自定义覆盖容器