【问题标题】:Laravel4: double post routeLaravel4:双张贴路线
【发布时间】:2014-08-04 13:16:34
【问题描述】:

对不起,如果听起来很奇怪,但是在路线上,在我看来,我需要做两个操作:

Route::post('booking', 'HomeController@booking');
Route::post('booking', function()
{
    return Queue::marshal();    
});

当然,这样做我会得到一个错误:无效数据。

然而对于视图“预订”的帖子我需要调用控制器的方法,同时返回Queue::marshal()

也许我可以做到这一点?

非常感谢!

编辑:

这里是 HomeController@booking 方法:

http://paste.laravel.com/19ej

【问题讨论】:

  • 你能发布你的HomeController@booking方法吗?
  • 好的,我已经编辑了问题

标签: laravel laravel-4 laravel-routing


【解决方案1】:

如果你用相同的动词和 url 定义两条路由,第二条路由永远不会被触发。

Route::post('booking', 'HomeController@booking'); // Laravel will find this route first
Route::post('booking', function()                 // so this function will never be executed
{
    return Queue::marshal();    
});

我看到您的HomeController@booking() 正在处理一个表单。为什么你可以为此使用另一条路线?

Route::post('booking/create', 'HomeController@booking');

然后改变你的表单action方法指向这条路线:

// this will render <form method="POST" action="http://yourdomain.com/booking/create"
{{ Form::open(array('action' => 'HomeController@booking')) }}

这样您就不会让一条路线与另一条路线重叠。


一些与问题无关的建议。看看你的控制器,我注意到你在检查错误时会这样做:

if ( ! $errorCondition) {
  // do stuff
} else {
  // show errors
}

如果你这样写,你的代码会更容易阅读:

if ($errorCondition) {
   // return error
}

// do stuff

【讨论】:

  • 谢谢曼努埃尔!它正在工作,但需要很长时间,比如 20 秒。很奇怪。可能是因为现在表格不完整。我在这里提出了另一个关于如何在 sintax Blade 中重写表单的问题:stackoverflow.com/questions/20120553/… 解决这个问题可能会开始一切顺利。
  • 好的,谢谢你,我已经找到了解决方案。你的推理很准确。只是有一个小问题,以这种方式发送了 2 封邮件而不是 1 封。所以我最后的解决方案是:Route::post('booking', function() { return Queue::marshal(); } );路线::post('rezervesana', 'HomeController@booking');一条是英国航线,另一条是拉脱维亚航线。我仍然不明白到底怎么可能,但是这样做一切正常。从两者接收队列并正确触发,并且在短时间内发送邮件。谢谢你的帮助!
猜你喜欢
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多