Laravel 向视图发送变量 [英] Laravel send variable to the view
问题描述
routes.php
Route::post("/{user}/{char_name}", array('作为' =>'char-profile-post','使用' =>'ProfileController@postDropDownList'));Route::get("/{user}/{char_name}", array('作为' =>'char-profile-get','使用' =>'ProfileController@getDropDownList'));
ProfileController.php
公共函数 postDropDownList($user, $char_name) {$user = Auth::user();$char_name = Input::get('选择');$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));}公共函数 getDropDownList($user, $char_name) {return View::make('layout.profile')->with('char_name', $char_name);}
来自 layout.profile 视图的片段
<块引用>
错误异常
未定义变量:char_name
(查看:C:wampwwwCaughtMiddle utorialappviewslayoutprofile.blade.php)
在编写以下内容时,我仍然收到此错误.
我的问题很明显.是我做错了什么,还是 Laravel 真的无法将几个变量从控制器发送到视图?
好的,这里是我部分解决我的问题的方法.
routes.php
Route::group(array('prefix' => 'user'), function(){Route::post('/{user}/{char_name}/selectedok', array('作为' =>'char-profile-post','使用' =>'ProfileController@postDropDownList'));Route::get('/{user}/{char_name}/selectedok', array('作为' =>'char-profile-get','使用' =>'ProfileController@getDropDownList'));});
ProfileController.php
公共函数 getDropDownList() {return View::make('layout.profile');}公共函数 postDropDownList($user, $char_name) {如果(身份验证::检查()){$user = Auth::user();$selected_char = Input::get('选择');$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 重定向.我将在视图中打印数据的唯一方法是在视图中执行此操作:
你选择的角色是 {{ 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()!如果我错了,请阻止我.
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);
}
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
Undefined variable: char_name
(View: C:wampwwwCaughtMiddle utorialappviewslayoutprofile.blade.php)
When writing the following I still receive this error.
<option> {{ $char_name['char_name'] }} </option>
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, so here is how I partially resolved my issue.
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);
}
}
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')}}
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
instead of this:
http://localhost/CaughtMiddle/tutorial/public/index.php/user/SerbanSpire/Spirescu/selectedok?Serban
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().
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屋!