【问题标题】:Dynamic / variable routing in LaravelLaravel 中的动态/可变路由
【发布时间】:2015-01-05 08:07:05
【问题描述】:

我试图让 Laravel 允许多个路由到 1 个控制器。这样做的原因是我想将我的代码用于多个项目。想象一下:

// Routes to the products
Route::get('/products', 'ProductController@index');
Route::get('/products/{id}', 'ProductController@product');
Route::get('/products/{id}/{any}', 'ProductController@product');

// Routes to the products
Route::get('/items', 'ProductController@index');
Route::get('/item/{id}', 'ProductController@product');
Route::get('/item/{id}/{any}', 'ProductController@product');

这对我来说似乎有点过头了 - 我尝试创建一个自定义配置文件并添加首选 uri 的

// custom config
return array(
    'product_plural' => 'products',
    'product_single' => 'product'
)

并将其添加到我的路线中

// Routes to the products
Route::get(Config::get('myconfig.product_plural'), 'ProductController@index');
Route::get(Config::get('myconfig.product_single').'/{id}', 'ProductController@product');
Route::get(Config::get('myconfig.product_single').'/{id}/{any}', 'ProductController@product');

这会导致

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException 

这让我相信配置在路由时不可用。显然我做错了什么,我现在有点不知所措。如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

  • 运行 php artisan routes 以确保您有您期望的路线,如果您仍然有问题,您应该在收到此错误时显示(您运行的 url)。
  • 谢谢@MarcinNabiałek - 这向我显示了我的自定义配置文件中的一个错误 - 并且能够解决这个问题,现在一切都按我的意愿工作。

标签: laravel routing


【解决方案1】:

您可能已经到达了您尚未定义的路线...

运行 php artisan routes 并检查您是否已将您点击的 url 注册为路由。

【讨论】:

  • 这正是 Marcin Nabiałek 在去年 11 月所说的,并且是正确的答案。不过还是谢谢。
  • 哦,对不起。我没有阅读 cmets... 请让 Marcin 将其发布为答案或只是接受我的答案,以供未来用户使用。否则,这会让他们更难
猜你喜欢
  • 2017-07-19
  • 1970-01-01
  • 2021-03-31
  • 2016-03-05
  • 2014-12-06
  • 2018-04-18
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
相关资源
最近更新 更多