【问题标题】:Laravel 5.2 controller index returns blank pageLaravel 5.2 控制器索引返回空白页
【发布时间】:2016-08-03 15:46:57
【问题描述】:

我创建了一条通往简单联系页面的路线。我使用控制器将数据保存在数据库中并将它们显示在同一页面上。当我提交表单时,我得到一个空白页面,但我希望用户保持联系。我试图传递索引视图,但随后出现错误。

文件是

route.php

Route::get('contact', 'ContactController@index');

Route::post('contact', 'ContactController@create');

ContactController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Contact;

class ContactController extends Controller
{
   public function index() {
         $contacts = Contact::orderBy('created_at', 'asc')->get();
         return view('/contact', [ 'contacts' => $contacts ]);
}
  public function create(Request $request) {
    $name = $request->input('name');
    $contact = new Contact;
    $contact->name = $name; 
    $contact->save();
    #return view('/contact');
}
}

Contact.php

<?php

Namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = ['name'];
}

?>

contact.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Contact page</div>

                <div class="panel-body">
                    {!! Form::open(array('url' => 'contact')) !!}
            {!! Form::label('name', 'Name') !!}
            {!! Form::text('name'); !!}
            {!! Form::submit('Submit'); !!}
            {!! Form::close() !!}
            @if (count($contacts) > 0)
             @foreach ($contacts as $contact) 
            {{ $contact->name }}
                @endforeach
                @endif
        </div>
            </div>
        </div>
    </div>
</div>
@endsection

【问题讨论】:

  • 只需 view('contact'.....) 去掉斜线即可!
  • 我刚刚写了与 Ismail RBOUH 相同的回复,说记住访问是用点而不是斜线,并且没有 Blade.php 表示法
  • 这并不能解决问题。
  • 你得到一个空白页,因为你已经注释掉了 return 声明。删除#。并删除斜线。
  • ..-&gt;save()之后使用return redirect()-&gt;back()而不是返回视图

标签: laravel


【解决方案1】:

尝试将contact.blade.php 放入views 文件夹并使用view('contact', [...]) 而不是view('/contact');,您不需要斜线并将return back() 添加到create 方法:

public function create(Request $request) {
    $name = $request->input('name');
    $contact = new Contact;
    $contact->name = $name; 
    $contact->save();
    return back();
}

【讨论】:

  • 它没有解决问题,因为我已经在正确的位置获得了视图,但我仍然得到一个空白页面。刷新浏览器后,我返回索引页面。
猜你喜欢
  • 2020-07-04
  • 2013-07-06
  • 1970-01-01
  • 2016-10-26
  • 2021-08-12
  • 2016-07-05
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多