传递变量进行查看

时间:2014-02-27 15:17:14

标签: view laravel blade

我想我可能发现了一个错误。这就是我现在所拥有的。

routes.php文件

Route::get('/{user}/{char_dynasty}/{char_name}/selectedok', array(
      'as' => 'char-profile-get',
    'uses' => 'CharacterController@getDropDownList'));   

Route::post('/user/{user}/{char_dynasty}/{char_name}/selectedok', array(
      'as' => 'char-profile-post',
    'uses' => 'CharacterController@postDropDownList'));  

CharacterController.php

class CharacterController extends BaseController{

    public function getDropDownList($user, $char_dynasty, $char_name) 
    {
        if(Auth::check()) 
        {
            $user = Auth::user();

            return View::make('layout.notification', array(
                        'user' => $user,
                        'char_dynasty' => $char_dynasty,
                        'char_name' => $char_name));
        }
        else 
        {
            return App::abort(404);
        }
    }

    public function postDropDownList() 
   {
        if (Auth::check())
        {   
            $user = Auth::user();
            $char_name = User::find($user->id)->characters()->where('char_name', '=', Input::get('choose'))->first();

            return Redirect::route('char-profile-get', array(Session::get('theuser'), 
                                                            $char_name->char_dynasty, 
                                                            $char_name->char_name));
        }
    }
}

profile.blade.php(摘录)

<form action="{{ URL::route('char-profile-post') }}" method="post">
    <select name="choose">
        <option value="choose">Choose a character</option>
        <option> {{ $c_name }} </option>
    </select>
    <input type="submit" value="Ok">
    {{ Form::token() }}
</form>

错误表明$char_dynasty未定义,通常使用

  <option> {{ $char_dynasty }} </option>

我更改为使用另一个变量填充下拉列表,以便能够从$char_name执行数据库查询postDropDownList

如果我在函数getDropDownList, var_dump($char_dynasty); var_dump($char_name)中执行以下操作

     string 'Spirescu' (length=8)
     string 'Ion' (length=3)

关键是getDropDownList中的参数具有正确的数据,但不会转移到视图。我究竟做错了什么?我不知道如何访问View中的变量?

我也试过return View::make('layout.notification', compact('user' , 'char_dynasty' , 'char_name' ));

  $data = array(
            'user' => $user,
            'char_dynasty' => $char_dynasty,
            'char_name' => $char_name);

  return View::make('layout.notification', $data);

我收到同样的错误。或$data未定义。

0 个答案:

没有答案