在我将我的Laravel web应用程序部署到我的主机后,我的代码不能像本地一样工作。我有一个表单和POST
到一个路由,它调用一个控制器处理任何类型的请求。我想对每种类型的请求做出反应,但是Request::Method()
函数返回GET
。
路线:
Route::any('/', [
'as' => 'root', 'uses' => 'WelcomeController@index']);
叶片:
<form action="{{ URL::route('root') }}" method="POST">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}">
<input type="hidden" name="newsId" value="{{ $newsId }}">
<input type="hidden" name="orders" id="orders" value="">
<button class="btn" name="next">NEXT</button>
<button class="btn" name="save">SAVE</button>
</form>
控制器:
if (Request::isMethod('get')) {
$newsId = (Auth::user()->last_news_id % 100) + 1;
$sentences = News::find($newsId)->sentences;
return view('summarizer')->with(['sentences' => $sentences, 'newsId' => $newsId]);
} elseif (Request::isMethod('POST')) {
return 'post';
}
我还使用了Request::Method()
,它一直返回GET
!我在我的本地主机上测试了这些代码,它工作得很好。
我很惊讶Input::all()也返回空值。
尝试这样做——在index()方法中添加Request类依赖注入。这样的
public function index(Request $request) {
if ($request->isMethod('get')) {
// your code
}elseif ($request->isMethod('post')) {
// your code
}
最好和最简单的方法是创建两个路由,
Route::get('/', [
'as' => 'root', 'uses' => 'WelcomeController@getFunction']);
Route::post('/', [
'as' => 'root', 'uses' => 'WelcomeController@postFunction']);
然后在控制器中创建两个函数。现在尝试在postFunction中添加dd(Input::all())来测试Input:all()是否仍然为空。
更重要的是,最好练习使用刀片创建您的表单:
{{ Form::open(['route' => 'root','method' => 'post']) }}
//
{{ Form::close() }}
我也遇到过类似的问题。将请求从http更改为https为我修复了它。在我的情况下,服务器强制请求https。