Laravel发送变量到视图 [英] Laravel send variable to the view

查看:158
本文介绍了Laravel发送变量到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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



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中获取代码片段查看



Snippet from layout.profile View

 <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>




ErrorException

未定义的变量:char_name

(查看:C:\wamp\www\CaughtMiddle\tutorial\app\views\layout\profile.blade.php)

ErrorException
Undefined variable: char_name
(View: C:\wamp\www\CaughtMiddle\tutorial\app\views\layout\profile.blade.php)

写下面的内容时,我仍然收到此错误。

When writing the following I still receive this error.

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

我的问题很明显。我做错了,或者Laravel真的不能从控制器发送几个变量到视图?

My question is pretty obvious. Am I doing something wrong, or Laravel is truly incapable to send a couple of variables from the controller to the view?

推荐答案

Ok ,所以这里是如何我部分解决我的问题。

Ok, so here is how I partially resolved my issue.

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



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



    }
}

是的我知道我重定向到'char-profile-get'与3参数,但我只需要2.它不打扰我。正如你所观察到的,我用 - >与重定向。我将在视图中打印数据的唯一方法是在视图中执行:

Yes I know I redirect to 'char-profile-get' with 3 parametres, but I only need 2. It doesn't bother me. As you observe, I redirect with ->with. The only way I will print the data in the view is by doing this in the view:

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

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

If I don't overwrite the variables in postDropDownList($user, $char_name) my URL will literally look like this:

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()也不打印。

So the verdict. The parametres in the functions will never receive any data. I don't even know how to describe them, the variables are inexistent, even if I redirect to the get route with the correct data. var_dump($user) or var_dump($char_name) doesn't print anything on the browser, dd($user) dd($char_name) also doesn't print anything nor does print_r().

我的解决方案是否可行?通过使用 - >重定向并在Session中存储数据?因为这个解决方案会驱使我使用Session :: get()alot!如果我错了,请停止我。

Is my solution a viable one? By redirecting using ->with and storing the data in Session? Because this solution will drive me into using Session::get() alot! Stop me if I am wrong.

这篇关于Laravel发送变量到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆