2014-05-19 77 views
0

我正在創建一個演示項目。目前,我正在爲用戶組件構建基礎知識。我在我的路線文件中創建了一個足智多謀的路線,通過工匠生成了一個UsersController,並開始填充代碼。Laravel:重定向到索引視圖不會觸發方法

在我UsersController我有一個簡單index方法:

public function index() 
{ 
    $users = User::all(); 
    return View::make('users.index')->with('users', $users); 
} 

新用戶形式的create方法:

public function create() 
{ 
    return View::make('users.create'); 
} 

而對於新用戶表單提交store方法:

public function store() 
{ 
    $user = new User; 
    $user->username = Input::get('username'); 
    $user->email  = Input::get('email'); 
    $user->nameFirst = Input::get('nameFirst'); 
    $user->nameLast = Input::get('nameLast'); 
    $user->password = Input::get('password'); 
    $user->save(); 

    Redirect::route('users.index'); 
} 

現在,所有這些都單獨工作。索引方法抓取所有用戶並在我的views/users/index.blade.php視圖中正確顯示,新用戶表單生成正確,當我提交表單時,它觸發store方法並將用戶數據寫入數據庫。

我遇到的問題是,當我嘗試使用Redirect::route('users.index');重定向回索引視圖時,我得到一個空白的瀏覽器窗口。我的網址按預期顯示,http://localhost:8001/users,但我得到一個完全空白的窗口: enter image description here

我閱讀了文檔,它看起來像我正確使用重定向路由。任何人都可以幫助弄清楚發生了什麼問題嗎?

我還應該補充說,無論我指向哪條路由,它都不會像重定向工作。無論我使用哪一個路由名稱,我仍然會看到空白的瀏覽器窗口。

編輯:這是我的觀點:

這裏是views/users/index.blade.php文件:

@extends('layouts.default') 

@section('title') 
User List 
@stop 

@section('content') 
    <table> 
     <thead> 
      <tr> 
      <th>Username</th> 
      <th>First</th> 
      <th>Last</th> 
      <th>Email</th> 
      </tr> 
     </thead> 
     <tbody> 
     @foreach($users as $user) 
      <tr> 
       <td>{{ link_to("https://stackoverflow.com/users/{$user->username}", $user->username) }}</td> 
       <td>{{ $user->nameLast }}</td> 
       <td>{{ $user->nameFirst }}</td> 
       <td>{{ $user->email }}</td> 
      </tr> 
     @endforeach   
     </tbody> 
    </table> 
    <div> 
     {{ HTML::linkAction('users.create', "Create User") }} 
    </div> 

@stop 

這裏的views/users/create.blade.php文件:

@extends('layouts.default') 

@section('content') 
    <h1>Create a New User</h1> 
    {{ @Form::open(['route' => 'users.store']) }} 
     <div> 
      {{ Form::label('nameFirst', "First Name") }} 
      {{ Form::text('nameFirst') }} 
     </div> 
     <div> 
      {{ Form::label('nameLast', "Last Name") }} 
      {{ Form::text('nameLast') }} 
     </div> 
     <div> 
      {{ Form::label('username', "Username") }} 
      {{ Form::text('username') }} 
     </div> 
     <div> 
      {{ Form::label('email', "Email") }} 
      {{ Form::text('email') }} 
     </div> 
     <div> 
      {{ Form::label('password', "Password") }} 
      {{ Form::password('password') }} 
     </div> 

     {{ Form::submit('Create User') }} 
    {{ @Form::close() }} 

@stop 

routes.php文件:

<?php 

Route::get('/', function() 
{ 
    return "home"; 
}); 

Route::resource('users', 'UsersController'); 

而且每php artisan routes路線:

+--------+-----------------------------+---------------+-------------------------+----------------+---------------+ 
| Domain | URI       | Name   | Action     | Before Filters | After Filters | 
+--------+-----------------------------+---------------+-------------------------+----------------+---------------+ 
|  | GET|HEAD/    |    | Closure     |    |    | 
|  | GET|HEAD users    | users.index | [email protected] |    |    | 
|  | GET|HEAD users/create  | users.create | [email protected] |    |    | 
|  | POST users     | users.store | [email protected] |    |    | 
|  | GET|HEAD users/{users}  | users.show | [email protected] |    |    | 
|  | GET|HEAD users/{users}/edit | users.edit | [email protected] |    |    | 
|  | PUT users/{users}   | users.update | [email protected] |    |    | 
|  | PATCH users/{users}   |    | [email protected] |    |    | 
|  | DELETE users/{users}  | users.destroy | [email protected] |    |    | 
+--------+-----------------------------+---------------+-------------------------+----------------+---------------+ 

回答

0

,您可以嘗試,它爲我工作

public function store() 
{ 
    ... 
    return Redirect::to('users'); 
} 
+0

我試過了,它給了相同的結果,一個空白頁。如果我點擊地址欄並點擊進入索引頁加載並顯示所有用戶。 –

+0

你可以顯示你的'index.blade.php'嗎?並檢查你的index.blade.php路徑是'app/views/users/index.php'?謝謝。 – lighter

+0

當然,我會在短短一秒內將它添加到原始文章中作爲編輯。 –

相關問題