【问题标题】:Pass parameter to create action in controller传递参数以在控制器中创建动作
【发布时间】:2018-02-04 16:34:00
【问题描述】:

在创建控制器中使用参数时,我在获取要呈现的页面时遇到问题。我的显示控制器工作,但我的创建控制器没有。抛出的错误是 404。

抱歉,找不到您要查找的页面。

网址是: http://myapp.test/country/us/state/create

我的控制器看起来像:

// Show
public function show(Country $country, State $state){
  return view('state.show', compact('state'));
}
// Create
public function create(Country $country) {
  return view('state.create', compact('country'));
}

我的路线如下:

Route::get('country/{country}/state/{state}', 'StateController@show');
Route::get('country/{country}/state/create', 'StateController@create');

【问题讨论】:

  • 请说明如何生成显示 404 的链接。
  • 网址是:http://myapp.test/country/us/state/create刚刚也编辑了帖子
  • 创建路由优先于状态路由
  • @DaveyContreras 谢谢。正如linktoahref 所说,将create 路由放在第一条路由之前。
  • 谢谢你们,解决了问题!!!哇哦……像这样的小事会让你毛骨悚然……这有什么关系?

标签: php laravel laravel-5 controller routes


【解决方案1】:

你需要翻转你的路线才能成为

Route::get('country/{country}/state/create', 'StateController@create');
Route::get('country/{country}/state/{state}', 'StateController@show');

Laravel 按照定义的顺序处理路由,因此在您当前的代码中,Laravel 将 create 视为 state

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多