【问题标题】:laravel post does not get picked up?laravel 的帖子收不到?
【发布时间】:2017-05-02 18:11:53
【问题描述】:

我正在尝试传递一个表单。我正在使用请求方法来获取变量。这是我的刀片形式:

<div class="add_photo">
<h1>Add a photo</h1>
    <form action="{{Route('postPhoto')}}">
        <span>Name: </span>
        <input type="text" name="title">
        <span>File: </span>
        <input type="text" name="file">
        <input type="submit" value="Add">
        <input type="hidden" name="_token" value="{{ Session::token() }}">
    </form>
</div>

涉及的路线:

Route::get('/admin/gallery', 'GalleryController@manageGallery')->name('manageGallery');
Route::post('/admin/gallery', 'GalleryController@postPhoto')->name('postPhoto');

这是我的控制器:

  class GalleryController extends Controller
{
    public function manageGallery() {
        return view('home.manageGallery');
    }

    public function postPhoto(Request $request) {
        die("works");
    }
}

它不会向我抛出错误。它什么也不做。所以我的问题是:我使用这种方法是错误的还是我需要更多的东西?提前致谢。

【问题讨论】:

  • 我不明白您希望这样做。你试过dd($request)里面postPhoto()
  • 我做到了。它不返回任何东西。
  • 我希望看到 postPhoto() 获取发布的变量。对不起,双重评论。

标签: php laravel post routes


【解决方案1】:

首先确保您使用的表单使用正确的方法用于您的路线

<div class="add_photo">
   <h1>Add a photo</h1>
   <form action="{{Route('postPhoto')}}" method="post">
        <span>Name: </span>
        <input type="text" name="title">
        <span>File: </span>
        <input type="text" name="file">
        <input type="submit" value="Add">
        <input type="hidden" name="_token" value="{{ Session::token() }}">
    </form>
</div>

在您的控制器中,将以下内容放入 postPhoto 函数中

public function postPhoto(Request $request)
{
    dd($request);
}

当您提交表单时,您现在应该会在屏幕上看到 Request 对象输出

【讨论】:

  • 谢谢。我的实际问题很简单。我没有指定方法。
【解决方案2】:

您可能想使用Blade Forms 以更自然的方式为 Laravel 制作表单

{{ Form::open(['route' => '/admin/gallery', 'method' => 'post', 'files' => true]) }}

    {{ Form::text('title') }}

    {{ Form::label('title', 'Name :') }}

    {{ Form::file('file') }}

    {{ Form::label('file', 'File :') }}

    {{ Form::submit('Add') }}

{{ Form::close() }}

它减少了自己添加令牌的开销,因为它是在使用 Form 门面时自动添加的。

然后,在您的控制器中,您可以在发送表单时进行类似的调试:

<?php

use Request; /* do not forget this line */

class GalleryController extends Controller
{
    public function postPhoto(Request $request)
    {
        dd($request->toArray());
    }
}

【讨论】:

  • tahnks。我得调查一下。
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 2014-04-26
  • 2020-06-14
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2015-03-25
相关资源
最近更新 更多