doujiao9426 2014-05-19 02:50
浏览 52
已采纳

Laravel:重定向到索引视图不会触发方法

I'm creating a demo project. At the moment I'm building the basics for a users component. I created a resourceful route in my routes file, generated a UsersController via artisan, and started filling in the code.

In my UsersController I have a simple index method:

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

A create method for the new user form:

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

And a store method for the new user form submission:

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');
}

Right now, all of these work individually. The index method grabs all of the users and displays them correctly in my views/users/index.blade.php view, the new user form generates correctly, and when I submit the form it fires the store method and writes the user data to the database.

The problem I'm having is that when I try to redirect back to the index view using Redirect::route('users.index'); I get a blank browser window. My url shows as expected, http://localhost:8001/users, but I get a completely blank window: enter image description here

I read through the docs and it looks like I'm using the redirect route correctly. Can anyone help figure out what's going wrong?

I should also add that it doesn't seem like the redirect works regardless of which route I point it to. No matter which one of the route name I use I still get blank browser window.

EDIT: Here are my views:

Here's the views/users/index.blade.php file:

@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("/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

Here's the views/users/create.blade.php file:

@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

The routes.php file:

    <?php

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

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

And the routes per php artisan routes:

+--------+-----------------------------+---------------+-------------------------+----------------+---------------+
| Domain | URI                         | Name          | Action                  | Before Filters | After Filters |
+--------+-----------------------------+---------------+-------------------------+----------------+---------------+
|        | GET|HEAD /                  |               | Closure                 |                |               |
|        | GET|HEAD users              | users.index   | UsersController@index   |                |               |
|        | GET|HEAD users/create       | users.create  | UsersController@create  |                |               |
|        | POST users                  | users.store   | UsersController@store   |                |               |
|        | GET|HEAD users/{users}      | users.show    | UsersController@show    |                |               |
|        | GET|HEAD users/{users}/edit | users.edit    | UsersController@edit    |                |               |
|        | PUT users/{users}           | users.update  | UsersController@update  |                |               |
|        | PATCH users/{users}         |               | UsersController@update  |                |               |
|        | DELETE users/{users}        | users.destroy | UsersController@destroy |                |               |
+--------+-----------------------------+---------------+-------------------------+----------------+---------------+
  • 写回答

2条回答 默认 最新

  • drmgg4411 2014-05-19 02:55
    关注

    You can try it, it work for me

    public function store()
    {
       ...
       return Redirect::to('users');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)