PHP Laravel 5.5中未正确触发表单操作

时间:2018-03-17 03:57:46

标签: php laravel laravelcollective

我正在使用Laravel Collective提供的表格并且已正确安装。 我要做的是在PictureController.php中验证表单 就像这样:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Picture;
use DB;

class PictureController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pictures = Picture::all();
        return view('welcome')->with('pictures', $pictures);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('pictures.publicize');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
          'hashtag' => 'required',
        ]);
        return 123;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这是包含此表单的publicize.blade.php的代码段。

{!! Form::open(array(
                                    'action' => 'PictureController@store',
                                    'method' => 'POST',
                                    'files' => true
                                  ))
                                !!}
                                  <div class="form-group">
                                    {{ Form::label('hashtag', 'HashTag') }}
                                    {{ Form::text('hashtag', '', ['placeholder' => 'Eg. #Happiness', 'class' => 'form-control', 'name' => 'hashtag' /*'required'*/]) }}
                                  </div>
                                    {{ Form::submit('Publicize', ['class' => 'btn btn-primary']) }}
                                {!! Form::close() !!}

routes/web.php中,这是代码编写的......

Route::resource('/', 'PictureController');

我还在GitHub上提交了完整的代码。只需查看this commit即可查看所有文件。

问题是...... 当我在浏览器中查看源代码时,我发现表单中的action属性等于主页,即/public/。看起来表单没有连接到我在publicize.blade.php中指定的控制器和方法。如何通过验证使该表单正常工作?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

更新路线,web.php(您应该尝试这种方式)

Route::resource('/', 'PictureController');

还更新动作属性,

{!! Form::open(array(
        'action' => '/',
        'method' => 'POST',
        'files' => true
      ))
!!}

来源:here

答案 1 :(得分:0)

你必须在路上给出一个名字, 例如:

Route::resource('picture', 'PictureController');

像你一样在你的刀片上设置,

{!! Form::open(array(
                     'action' => 'picture',
                     'method' => 'POST',
                     'files' => true
              ))
!!}