【问题标题】:Laravel send variable to the viewLaravel 将变量发送到视图
【发布时间】:2014-03-28 11:26:44
【问题描述】:

routes.php

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

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

ProfileController.php

public function postDropDownList($user, $char_name) {

$user = Auth::user();
$char_name = Input::get('choose');
$char_name_obj = User::find($user->id)->characters()->where('char_name', '=', $char_name)->first();

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

  }


public function getDropDownList($user, $char_name) {

return View::make('layout.profile')
            ->with('char_name', $char_name);
 }

layout.profile 视图中的片段

 <form action="{{ URL::route('char-profile-post') }}" method="post">
         <select name="choose">
            <option value="choose">Choose a character</option>

                 @foreach($char_name as $c)
                 <option> {{ $c->char_name }} </option>
                 @endforeach

         </select>
         <input type="submit" value="Ok">
            {{ Form::token() }}
        </form>

错误异常
未定义变量:char_name
(查看:C:\wamp\www\CaughtMiddle\tutorial\app\views\layout\profile.blade.php)

在编写以下内容时,我仍然收到此错误。

<option> {{ $char_name['char_name'] }} </option>

我的问题很明显。是我做错了什么,还是 Laravel 真的无法将几个变量从控制器发送到视图?

【问题讨论】:

  • 你的意思是把它放在这里?公共函数 getDropDownList($user, $char_name) { var_dump($char_name);返回视图::make('layout.profile') ->with('char_name', $char_name); } 它什么也不做。同样的错误,没有其他消息。
  • 我已经从视图中删除了下拉列表,并按照你告诉我的做了。该变量似乎为空。它不会在浏览器上写任何东西。但为什么它是空的?
  • 这个变量来自您的第二个 URL 参数。这就是你在路线中写的:Route::get("/{user}/{char_name}", array(。你在 URL 中传递了什么?您是否发布了完整的getDropDownList 功能?你想完成什么?
  • 是的。我有一个查询数据库的 post 路由和一个尝试将新值带到视图的 get 路由。首先,我想用数据库中的值填充一个下拉列表。其次,我想获取选定的值并将其写入视图中。类似的东西。您选择了 {{$char_name}}。这背后的想法是我将尝试将 $char_name 附加到新 URL。我需要这样做,因为我需要那个变量。我知道如何填充下拉列表,但该解决方案不适合我正在尝试做的事情。
  • 我总是试图绕过我现在正在做的事情,直接从视图中查询数据库等等,但仅此而已。这真的很令人沮丧,因为由于这个问题我无法组织我的代码。我可以给你一个有效的例子,我不知道为什么。

标签: laravel controller blade


【解决方案1】:

Laravel 绝对可以将多个变量发送到一个视图,这里有一些代码展示了我如何在我的项目中做到这一点:

路线:

Route::get('play/{id}', array('before' => 'loggedin', 'uses' => 'GameController@confirmEntry'));

如您所见,在这条路线中,我希望将变量 id 传递给我的控制器。

控制器:

public function confirmEntry($id){
    //stuff that doesn't matter
    $data = array('puzzle' => $puzzle,
                  'user'   => $user,
                  'wallet' => $wallet);
    return View::make('game.solver-confirm', $data);
}

在上面的示例中,由于我将$id 列为参数,并且我的路由中有id,所以$id 将设置为路由中的任何内容。例如,如果我去play/56$id 将等于56

查看:

@foreach ($puzzle->tile as $tile)
    <li>a list gets made here</li>
@endforeach

您可以在控制器中看到,我将$puzzle(连同其他一些变量)传递给视图,然后像您在我的视图中显示的那样调用它们。

您的问题:

我看到的第一个问题是您从路由中接受两个变量,然后覆盖它们。这对我来说没有多大意义:

public function postDropDownList($user, $char_name) {
    $user = Auth::user();
    $char_name = Input::get('choose');
    //more stuff here
}

为什么要传递 $user$char_name 只是为了覆盖它们?

这让我觉得您遇到的问题是因为您的数据结构不佳。我建议做一个var_dumpprint_r 看看$char_name 的实际结构。

参考文献:

【讨论】:

    【解决方案2】:

    好的,这就是我部分解决问题的方法。

    routes.php

    Route::group(array('prefix' => 'user'), function()
    {       
    
        Route::post('/{user}/{char_name}/selectedok', array(
            'as' => 'char-profile-post',
            'uses' => 'ProfileController@postDropDownList'));   
    
        Route::get('/{user}/{char_name}/selectedok', array(
            'as' => 'char-profile-get',
            'uses' => 'ProfileController@getDropDownList'));                 
    
    });
    

    ProfileController.php

    public function getDropDownList() {  
    
            return View::make('layout.profile');     
    
        }
    
    
    
    public function postDropDownList($user, $char_name) {
    
        if (Auth::check())
        {   
            $user = Auth::user();
            $selected_char = Input::get('choose');
            $char_name = User::find($user->id)->characters()->where('char_name', '=', $selected_char)->first();
    
    
                return Redirect::route('char-profile-get', array($user->username, $char_name->char_dynasty, $char_name->char_name))
                        ->with('user', $user->username)
                        ->with('charname', $char_name->char_name)
                        ->with('dynastyname', $char_name->char_dynasty);
    
    
    
        }
    }
    

    是的,我知道我使用 3 个参数重定向到“char-profile-get”,但我只需要 2 个。它不会打扰我。如您所见,我使用 -> with 重定向。我将在视图中打印数据的唯一方法是在视图中执行此操作:

     Your chosen character is {{ Session::get('charname') }} {{ Session::get('dynastyname')}}
    

    如果我不覆盖 postDropDownList($user, $char_name) 中的变量,我的 URL 将如下所示:

    http://localhost/CaughtMiddle/tutorial/public/index.php/user/%7Buser%7D/%7Bchar_name%7D/selectedok
    

    而不是这个:

    http://localhost/CaughtMiddle/tutorial/public/index.php/user/SerbanSpire/Spirescu/selectedok?Serban
    

    所以判决。函数中的参数永远不会收到任何数据。我什至不知道如何描述它们,变量是不存在的,即使我使用正确的数据重定向到 get 路由。 var_dump($user) 或 var_dump($char_name) 不会在浏览器上打印任何内容,dd($user) dd($char_name) 也不打印任何内容,print_r() 也不会。

    我的解决方案可行吗?通过使用 ->with 重定向并将数据存储在 Session 中?因为这个解决方案会促使我大量使用 Session::get() !如果我错了,请阻止我。

    【讨论】:

    • 你在哪里设置会话变量?
    猜你喜欢
    • 2023-03-22
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多