Laravel 5.2在不同页面中有两种形式,但方法和控制器相同

时间:2016-09-10 13:16:55

标签: php laravel laravel-5 laravel-5.2

我正在使用 Laravel 5.2 ,我在不同的页面中有两种形式,但方法和控制器相同..

两者都使用public function store(Request $request){}方法。

  

'show.blade.php'页面中的第一个表格:

<form action="{{ url('projects') }}" name="comment_form" class="row" method="POST">
{{ csrf_field() }}
<div class="col-md-3">
    <input placeholder="Name" name="name" type="text">
</div>
<div class="col-md-3">
    <input placeholder="Email" name="email" type="text">
</div>
<div class="col-md-3">
    <input placeholder="Subject" name="subject" type="text">
</div>
<div class="col-md-3">
    <input value="Id Number : {{ $project -> id }}" name="project_id" type="text" readonly>
</div>
<div class="col-md-12">
    <textarea placeholder="Comments" name="comments" cols="10" rows="10"></textarea>
</div>
<div class="col-md-12">
    <input type="submit" value="Send Comments">
</div>
</form>
  

'create.blade.php'页面中的第二个表单:

{!! Form::open(array('route' => 'projects.store', 'files' => true, 'name' => 'project_form')) !!}
    {{ Form::label('title', 'Title:', ['class' => 'top-bottom-margin']) }}
    {{ Form::text('title', null, ['class' => 'form-control', 'maxlength' => '255']) }}

    {{ Form::label('image', 'Image: ', ['class' => 'top-bottom-margin']) }}
    {{ Form::file('image', ['accept' => 'image/*']) }}

    {{ Form::label('second_image', 'Optional Image: ', ['class' => 'top-bottom-margin']) }}
    {{ Form::file('second_image', ['accept' => 'image/*']) }}

    {{ Form::label('third_image', 'Optional Image: ', ['class' => 'top-bottom-margin']) }}
    {{ Form::file('third_image', ['accept' => 'image/*']) }}

    {{ Form::label('body', 'Body:', ['class' => 'top-bottom-margin']) }}
    {{ Form::textarea('body', null, ['class' => 'form-control']) }}

    {{ Form::submit('Create Project', ['class' => 'btn btn-success btn-lg btn-block top-bottom-margin']) }}
{!! Form::close() !!}
  

ProjectsController.php方法代码:

public function store(Request $request)
{
    $data = $request->all();
    if(isset($data['project_form'])){
    // validation START
    $this -> validate($request, array(
        'title' => 'required | max:255',
        'body' => 'required',
        'image' => 'required'
    ));
    // validation END

    // storing in database
    $project = new Project;

    $project -> title = $request -> title;
    $project -> body = $request -> body;


    // First images START
    $image = $request -> file('image');
    $fileName = time() . '.' . $image -> getClientOriginalExtension();
    $location = public_path('admin_images/' . $fileName);
    Image::make($image) -> resize(860, 600) -> save($location);

    //database store
    $project -> image = $fileName;
    //First images END


    if ($request->hasFile('second_image')){
        // second images START
        $rand_number = rand(100, 2000);
        $second_image = $request -> file('second_image');
        $secondFileName = time() . $rand_number . '.' . $second_image -> getClientOriginalExtension();
        $secondLocation = public_path('admin_images/' . $secondFileName);
        Image::make($second_image) -> resize(860, 600) -> save($secondLocation);

        //database store
        $project -> second_image = $secondFileName;
        // second images END
    }


    if ($request->hasFile('third_image')){
        // third images START
        $second_rand_number = rand(3000, 5000);
        $third_image = $request -> file('third_image');
        $thirdFileName = time() . $second_rand_number . '.' . $third_image -> getClientOriginalExtension();
        $thirdLocation = public_path('admin_images/' . $thirdFileName);
        Image::make($third_image) -> resize(860, 600) -> save($thirdLocation);

        //database store
        $project -> third_image = $thirdFileName;
        // third images END
    }


    $project -> save();

    Session::flash('success', 'Your project was created successfully!');
    return redirect() -> route('projects.show', $project -> id);
}

现在我检查是否提交了 comment_form 或提交了 project_form

2 个答案:

答案 0 :(得分:0)

您可以创建隐藏元素来传递变量:

val func = new Function1[()=>Boolean, Int] {
             def apply(fn:()=>Boolean):Int = { fn(); 100 }
           }

>>> func: (() => Boolean) => Int = <function1>

func (new Function0[Boolean] {
        def apply():Boolean  = { println ("returning true"); true }
      })

>>> returning true
>>> res0: Int = 100

然后在控制器中获取它:

{!! Form::hidden('from', 'someView') !!}

此外,您可以通过使用会话和检索上一页网址来传递变量,但我认为隐藏的表单元素是您使用表单时的最佳选择。

答案 1 :(得分:0)

您可以将名称和值添加到提交按钮并检查。

if ($request->get('submit') == 'project') {
  // do something
} elseif ($request->get('submit') == 'comment') {
  // do something else
}

或使用开关

switch ($request->get('submit')) {
  case 'project':
    // do something
    break;

  case 'comment':
    // do something else
    break;

  default:
    // do this if above does not apply
    break;
}